开发者

ComboBox SelectedItem does not change after clearing the observable collection

I'm having an issue with a ComboBox which is bound to an ObservableCollection and I was wondering if anyone can point to what I am missing.

I have a ComboBox which is bound to a simple ObservableCollection<string>. Also I bind the SelectedIndex in a OneWay binding to some property.

In my application I get to a point where I want to clear the collection and repopulate it with different data and setting the SelectedIndex to a new value. for some reason the SelectedIndex binding does not work.

I'm attaching a little repro of the problem:

public partial class Window1 : Window, INotifyPropertyChanged
{
    private int j;
    public event PropertyChangedEventHandler PropertyChanged;

    public Window1()
    {
        InitializeComponent();
        DataContext = this;
        Tables = new ObservableCollection<string>();
    }

    public ObservableCollection<string> Tables { get; set; }

    private int _TheIndex;
    public int TheIndex
    {
        get { return _TheIndex; }
        set
        {
            _TheIndex = value;
            if (PropertyChanged != null)
            {
                PropertyChanged.Invoke(this, new PropertyChangedEventArgs("TheIndex"));
            }
        }
    }

    private void aaaa(object sender, RoutedEventArgs e)
    {
        j = (j + 1)%10;
        Tables.Clear();
        for(int i = 0; i < 10 ; i++)
        {
            Tables.Add(i.ToString());
        }
        TheIndex = j;
    }
}

the xaml is :

<Window x:Class="WpfApplication1.Window1"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="Window1" Height="300" Width="300">
    <Grid>
        <StackPanel>
            <ComboBox x:Name="TablesCombobox"
                      ItemsSource="{Binding Tables}"
        开发者_运维问答              SelectedIndex="{Binding TheIndex, Mode=OneWay}"/>
            <Button Content="asdasd" Click="aaaa"/>
        </StackPanel>
    </Grid>
</Window>


The problem is entirely caused by the Tables.Clear() line in your aaaa() method. Since Tables is an observable collection, wiping out all of the contents of the collection causes WPF to update the display with a new, empty list. Then it tries to select the currently active item using SelectedIndex, which does not exist (because the list is now empty). As a result, the binding engine is left with a value that cannot be applied, and decides to deactivate and detach the binding logic:

System.Windows.Data Warning: Got PropertyChanged event from Window1 for TheIndex
System.Windows.Data Warning: GetValue at level 0 from Window1 using DependencyProperty(TheIndex): '1'
System.Windows.Data Warning: TransferValue - got raw value '1'
System.Windows.Data Warning: TransferValue - using final value '1'
System.Windows.Data Warning: Deactivate
System.Windows.Data Warning: Replace item at level 0 with {NullDataItem}
System.Windows.Data Warning: Detach

By the time it gets to the 'TheIndex = j;' line, the binding is no longer active and does not see the change to TheIndex, which means that desired index is no longer selected.

There are a couple of solutions to solve this:

  1. Don't blow away the entire collection every time. Without clearing the collection, the data binding logic always has an index to select, meaning it never detaches.
  2. Use a TwoWay binding. This works because now the ComboBox participates in binding; you clear Tables, the binding tries to set but can't find the index so the ComboBox resets to the special 'no index' position of -1, which then writes back to TheIndex (the two-way part), which is a valid value so the binding logic doesn't detach.
  3. Select no index (-1) before clearing the collection. If no index (-1) is selected when Tables is cleared, then ComboBox doesn't try to apply SelectedItem, which means it doesn't 'see' the collection emptied and re-filled, and therefore, doesn't detach.

    private void aaaa(object sender, RoutedEventArgs e)
    {
        TheIndex = -1;
        j = (j + 1)%10;
        Tables.Clear();
        for (int i = 0; i < 10; i++)
        {
            Tables.Add(i.ToString());
        }
        TheIndex = j;
    }
    

For performance, architectural, and clarity reasons, I'd highly recommend option 1, though I realize that your actual scenario may be more complex and require something along the lines of 3.


Sidenote:

Locating the reason behind binding issues like this is reasonably easy when using binding traces like the one posted above. Turn them on for a single binding by declaring the System.Diagnostics namespace and adding PresentationTraceSources.TraceLevel=Highto the binding that is causing trouble:

<Window xmlns:diag="clr-namespace:System.Diagnostics;assembly=WindowsBase" />
...
<TextBlock Text="{Binding Path=x, diag:PresentationTraceSources.TraceLevel=High}" />

More ways of debugging WPF bindings are here.


I Know this is an old question but I have just experienced this problem myself so have written a helper method based on option 1 of @Nicholas Armstrong 's answer and thought I would share it, hope somebody will find it useful:

public void refreshDropdownOptions(ObservableCollection<object> OldOptions, ObservableCollection<object> NewOptions)
{
    MainWindow application = Application.Current.MainWindow as MainWindow;

    int highestCount = 0;

    if(OldOptions.Count() > NewOptions.Count())
    {
        highestCount = OldOptions.Count();
    }
    else
    {
        highestCount = NewOptions.Count();
    }

    for (int i = 0; i < highestCount; i++)
    {   
        if(i < OldOptions.Count() && i < NewOptions.Count())
        {// If we have not exceeded the count of either list, copy the new value over the old
            application.Dispatcher.Invoke((Action)(() => OldOptions[i] = NewOptions[i]));                   
        }
        else if (i < OldOptions.Count() && i >= NewOptions.Count())
        {// If we have no more new options remove the old option
            application.Dispatcher.Invoke((Action)(() => OldOptions.RemoveAt(i)));
            highestCount = OldOptions.Count();
            i--;
        }
        else if (i >= OldOptions.Count() && i < NewOptions.Count())
        {// if we have no more old options to replace, add the new option to the end of the collection
            application.Dispatcher.Invoke((Action)(() => OldOptions.Add(NewOptions[i])));
        }
    }
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜