none
WPF AutoCompleteBox SelectedItem Binding Issue RRS feed

  • Question

  • I have been struggling with this for a few days.

    When i set the value of a bound selecteditem to an autocompletebox in the window initialisater the value is not displayed, but if I display an message box in the dependencyproperty (that is bound to the selecteditem) on changed event then the value is displayed in autocompletebox.

    I have attached code of full example, run code and you will notice New Caledonia is displayed in the autocompletebox, then commment out the messagebox and you will see the New Caledonia is no longer displayed.

    I dont understand?

    XAML Code

    <Window x:Class="AutoCompleteBoxProblem.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:local="clr-namespace:AutoCompleteBoxProblem" xmlns:toolkit="clr-namespace:System.Windows.Controls;assembly=System.Windows.Controls.Input.Toolkit"
            Name="this" Title="MainWindow" Width="525" Height="350">
        <StackPanel Orientation="Vertical">
            <toolkit:AutoCompleteBox Width="100" Height="30" IsManipulationEnabled="False"
                                     ItemsSource="{Binding ElementName=this,
                                                           Path=Countries}"
                                     SelectedItem="{Binding ElementName=this,
                                                            Path=Country,
                                                            Mode=TwoWay,
                                                            UpdateSourceTrigger=LostFocus,
                                                            ValidatesOnExceptions=True,
                                                            NotifyOnValidationError=True,
                                                            ValidatesOnDataErrors=True}"
                                     ValueMemberBinding="{Binding CountryDesc}">
                  </toolkit:AutoCompleteBox>
           
        </StackPanel>
    </Window>

    Code Behind XAML

    using System.Collections.Generic;
    using System.Windows;
    using System.Linq;
    using System;
    namespace AutoCompleteBoxProblem
    {
        public partial class MainWindow : Window
        {
            public MainWindow()
            {
                InitializeComponent();
                List<Country> countries = new List<Country>();
                countries.Add(new Country(1, "Australia"));
                countries.Add(new Country(2, "Austria"));
                countries.Add(new Country(3, "New Zealand"));
                countries.Add(new Country(4, "New Caledonia"));
                countries.Add(new Country(5, "United Kingdom"));
                countries.Add(new Country(6, "United Arab Emirates"));
                countries.Add(new Country(7, "United States of America"));
                Countries = countries;
                Country = countries[3];
            }
            //Countries
            public static readonly DependencyProperty CountriesProperty = DependencyProperty.Register("Countries", typeof(List<Country>), typeof(MainWindow));
            public List<Country> Countries
            {
                get
                {
                    return (List<Country>)GetValue(MainWindow.CountriesProperty);
                }
                set
                {
                    SetValue(MainWindow.CountriesProperty, value);
                }
            }
            //Country
            public static readonly DependencyProperty CountryProperty = DependencyProperty.Register("Country", typeof(Country), typeof(MainWindow), new PropertyMetadata(CountryPropertyChangeCallback));
            public Country Country
            {
                get
                {
                    return (Country)GetValue(MainWindow.CountryProperty);
                }
                set
                {
                    SetValue(MainWindow.CountryProperty, value);
                }
            }
            private static void CountryPropertyChangeCallback(DependencyObject sender, DependencyPropertyChangedEventArgs e)
            {
                MainWindow addressControl = (MainWindow)sender;
                Country oldCountry = (Country)e.OldValue;
                addressControl.OnCountryChanged(oldCountry);
            }
            protected virtual void OnCountryChanged(Country oldCountry)
            {
                MessageBox.Show("WHY !!!");    
            }
        }
        
    }

    Country Class

    using System;
    namespace AutoCompleteBoxProblem
    {
        public class Country
        {
            public Country(int countryId, String countryDesc) 
            {
                CountryId = countryId;
                CountryDesc = countryDesc;
            }
            public int CountryId { get; set; }
            public String CountryDesc { get; set; }
        }
    }


    Simon

    Thursday, May 10, 2012 6:11 AM

Answers

  • Hi TFS 2010 and VS 2008,

    Yes, I have repro this behavior local, the cause of that issue is the template of AutoCompleteBox will update the Text property second time.

    On the other hand, the SelectedItem binding works correctly, and all binding stuff work well, you could try to use below workaround, check if SelectedItem is null in autocompletebox loaded event:

    private void AutoCompleteBox_Loaded(object sender, RoutedEventArgs e)
    {
        if (((AutoCompleteBox)sender).SelectedItem != null)
            ((AutoCompleteBox)sender).Text = ((((AutoCompleteBox)sender).SelectedItem as Country).CountryDesc);
    }

    addtional, might your issue is the same with this one:

    https://connect.microsoft.com/VisualStudio/feedback/details/595640/autocompletebox-does-not-update-visible-text#

    Best regards,


    Sheldon _Xiao[MSFT]
    MSDN Community Support | Feedback to us
    Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Friday, May 11, 2012 7:10 AM
    Moderator

All replies

  • Simon;

     Where is the xmlns declaration for toolkit?  I don't see it in the xaml

     <toolkit:AutoCompleteBox >


    JP Cowboy Coders Unite!

    Thursday, May 10, 2012 1:28 PM
  • Mr javaman II the declararion is at the end of the first line.

    xmlns:toolkit="clr-namespace:System.Windows.Controls;assembly=System.Windows.Controls.Input.Toolkit"


    Simon

    Thursday, May 10, 2012 10:51 PM
  • Where does that toolkit come from codeplex?  Which one?

    JP Cowboy Coders Unite!

    Friday, May 11, 2012 1:14 AM
  • Sorry should have thought of that.

    http://wpf.codeplex.com/releases/view/40535


    Simon

    Friday, May 11, 2012 2:09 AM
  • Hi TFS 2010 and VS 2008,

    Yes, I have repro this behavior local, the cause of that issue is the template of AutoCompleteBox will update the Text property second time.

    On the other hand, the SelectedItem binding works correctly, and all binding stuff work well, you could try to use below workaround, check if SelectedItem is null in autocompletebox loaded event:

    private void AutoCompleteBox_Loaded(object sender, RoutedEventArgs e)
    {
        if (((AutoCompleteBox)sender).SelectedItem != null)
            ((AutoCompleteBox)sender).Text = ((((AutoCompleteBox)sender).SelectedItem as Country).CountryDesc);
    }

    addtional, might your issue is the same with this one:

    https://connect.microsoft.com/VisualStudio/feedback/details/595640/autocompletebox-does-not-update-visible-text#

    Best regards,


    Sheldon _Xiao[MSFT]
    MSDN Community Support | Feedback to us
    Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Friday, May 11, 2012 7:10 AM
    Moderator
  • I am using mvvm so not to keen on writing code behind, althought the code is directly related to the interface which i guess makes it alright. I know my example is using code behind but this was just so you could replicate the issue.

    Your solution works, the issue in the link sounds similar although it talks about multiple autocompleteboxes on multiple tabs, which is different to my issue. Although I can see this being an issue for me in the near future.

    Perhaps I should raise this as a bug?

    Thanks Sheldon for your help again.


    Simon

    Friday, May 11, 2012 7:41 AM
  • Ok, you could submit this issue on connect:https://connect.microsoft.com/VisualStudio, might you could get better workaround on that site.

    Best regards,


    Sheldon _Xiao[MSFT]
    MSDN Community Support | Feedback to us
    Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Friday, May 11, 2012 7:45 AM
    Moderator