views:

237

answers:

1

Hi, I'm having an issue witha combobox whixh is bounded to an observable collection and I was wondering if anyone can point to what am i missing.

I have a combobox which is bounded to a simple ObservableCollection 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 repopulate it with different data and setting the selected index to a new value. for some reason the selected index 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>

A: 

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.

Nicholas Armstrong