locked
Problem binding AutoCompleteBox to dictionary RRS feed

  • Question

  • I am having a problem using AutoCompleteBox (ACB) like a Combobox, where,  the ACB can be bound to a dictionary of agencies with agecny codes (string) and agency names(string). 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>

    Control's 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 KeyTag
            {
                get { return codeValueBox.GetBindingBLOCKED EXPRESSION.ParentBinding; }
                set { codeValueBox.SetBinding(AutoCompleteBox.TagProperty, value); }
            }
    
            public int MinimumPrefixLength
            {
                get {return codeValueBox.MinimumPrefixLength; }
                set { codeValueBox.MinimumPrefixLength = value; }
            }
    
            //Move this method to MVVM behaviours.
            private void UpdateTextBinding(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.

    Also, I do not want to use converter for Text property of ACB.

    Any insight is greatly appreciated!!!

    Thursday, May 19, 2011 10:04 PM

Answers

  • Got a duplicate thread here.  http://forums.silverlight.net/forums/t/229293.aspx



    Tuesday, May 24, 2011 4:23 AM

All replies

  • Anybody!!!???

    Friday, May 20, 2011 1:58 PM
  • Got a duplicate thread here.  http://forums.silverlight.net/forums/t/229293.aspx



    Tuesday, May 24, 2011 4:23 AM