locked
Binding AutoCompleteBox to dictionary issues RRS feed

  • Question

  • I am trying to use the AutoCompleteBox (ACB) like a Combobox, where,  the ACB can be bound to a dictionary of agencies with agecny codes and agency names. The text should display the agency name but the associated agency code needs supplied for the selected ACB item. I have wrapped the ACB in a user control as below :

    XAML

    <UserControl xmlns:sdk="http://schemas.microsoft.com/winfx/2006/xaml/presentation/sdk"  
        x:Class="MySLProject.Views.Controls.CodeValueAutoCompletionBox"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        mc:Ignorable="d" x:Name="CodeValueAutoBox">
        <Grid x:Name="LayoutRoot" Background="White" >
            <sdk:AutoCompleteBox Height="23" HorizontalAlignment="Stretch" Name="codeValueBox" IsDropDownOpen="True" IsTextCompletionEnabled="True"
                                                 ValueMemberBinding="{Binding Path=Value}" FilterMode="ContainsOrdinal" ValueMemberPath="Value"
                                                 LostFocus="UpdateCode" VerticalAlignment="Top" >
                <sdk:AutoCompleteBox.ItemTemplate>
                    <DataTemplate>
                        <StackPanel Orientation="Horizontal" >
                            <TextBlock Text="{Binding Key}" />
                            <TextBlock Text=" - " />
                            <TextBlock Text="{Binding Value}" />
                        </StackPanel>
                    </DataTemplate>
                </sdk:AutoCompleteBox.ItemTemplate>
            </sdk:AutoCompleteBox>
        </Grid>
    </UserControl>

    Code behind:

    using System.Collections;
    using System.Collections.Generic;
    using System.Windows;
    using System.Windows.Controls;
    using System.Windows.Data;
    
    namespace MySLProject.Views.Controls
    {
        public partial class CodeValueAutoCompletionBox : UserControl
        {
            public CodeValueAutoCompletionBox()
            {
                InitializeComponent();
            }
            
            public Binding ItemsSource
            {
                get { return codeValueBox.GetBindingBLOCKED EXPRESSION.ParentBinding; }
                set { codeValueBox.SetBinding(AutoCompleteBox.ItemsSourceProperty, value); }
            }
    
            public Binding Text
            {
                get { return codeValueBox.GetBindingBLOCKED EXPRESSION.ParentBinding; }
                set { codeValueBox.SetBinding(AutoCompleteBox.TextProperty, value); }
            }
    
            public Binding SelectedItem
            {
                get { return codeValueBox.GetBindingBLOCKED EXPRESSION.ParentBinding; }
                set { codeValueBox.SetBinding(AutoCompleteBox.SelectedItemProperty, value); }
            }
    
            public Binding KeyTag
            {
                get { return codeValueBox.GetBindingBLOCKED EXPRESSION.ParentBinding; }
                set { codeValueBox.SetBinding(AutoCompleteBox.TagProperty, value); }
            }
    
            public int MinimumPrefixLength
            {
                get {return codeValueBox.MinimumPrefixLength; }
                set { codeValueBox.MinimumPrefixLength = value; }
            }
    
            private void UpdateCode(object sender, RoutedEventArgs e)
            {
                //Not checking for the type of the sender, as this user has only one control which is AutoCompleteBox.
                KeyValuePair<string, string> selectedItem;
    
                if (codeValueBox.SelectedItem != null)
                {
                    selectedItem = (KeyValuePair<string, string>)codeValueBox.SelectedItem;
                    codeValueBox.Tag = selectedItem.Key;
                }
    
                BindingExpression binding = codeValueBox.GetBindingBLOCKED EXPRESSION;
    
                if (binding != null)
                    binding.UpdateSource();
            }
        }
    }

    And here is how I am using this control in the my form:

    <localControls:CodeValueAutoCompletionBox Grid.Row="1" Grid.Column="6" x:Name="codeValAgency" HorizontalAlignment="Stretch"
                                        ItemsSource="{Binding Path=AgenciesLookup}" MinimumPrefixLength="0"
                                         KeyTag="{Binding Path=AgencyCode, Mode=TwoWay, ValidatesOnDataErrors=True, UpdateSourceTrigger=Explicit}"/>


    I have been able to take care of setting the selected AgencyCode to the bound class property, but am unable to display the agency name in the ACB text, given an agency code. I need to display the agency name when the form loads up for the first time and before the user starts editing the data.

    Any insight is greatly appreciated!!!

    Wednesday, May 18, 2011 10:40 PM

All replies

  • Hi,

    I am not sure Dictionary is Key pair - Value. Are you sure Dictionary support ItemsControl?

    Wednesday, May 18, 2011 11:38 PM
  • Hi,

    I am not sure Dictionary is Key pair - Value. Are you sure Dictionary support ItemsControl?

    I have been able to see the key value pairs in the drop-down appearing as 'A1 - Agency 1' and so on. And when I select one of the entries, the value from the dictionary i.e. 'Agency 1' is shown in the text. However the problem is, given the dictionary key, agency code ('A1'), how can the dictionary value ('Agency 1') be displayed in text.

    Thursday, May 19, 2011 12:35 AM
  • Did you try with DisplayMemberPath property?

    Thursday, May 19, 2011 2:47 AM
  • Did you try with DisplayMemberPath property?

    DisplayMemberPath is not available in AutoCompleteBox. It is there in Combobox only.

    Thursday, May 19, 2011 11:04 AM
  • How's your issue going now?  How about use ComboBox instead?  For example, http://www.codeproject.com/KB/silverlight/AutoComplete_ComboBox.aspx

    Tuesday, May 24, 2011 4:02 AM
  • How's your issue going now?  How about use ComboBox instead?  For example, http://www.codeproject.com/KB/silverlight/AutoComplete_ComboBox.aspx

    My issue is still at the same spot Smile. I am using ComboBox for all other purposes, especially where the drop down contents are small in number. But for this particular case I need filetering while typing the text. Reason, the total number of items is around 18K, and the user might want that his/her typed text could be anywhere in an item. Hence filtering based on text will show only the matches.giving user a clear picture of which item s/he wants to choose.

    I have seen this sample referred by you before too, but have never explored it. Will see how it can solve this problem. Thank you for the link. Smile

    Wednesday, May 25, 2011 12:36 PM