清除可观察集合后 ComboBox SelectedItem 不会更改

发布于 2024-08-18 04:23:56 字数 1963 浏览 11 评论 0原文

我遇到了与 ObservableCollection 绑定的 ComboBox 问题,我想知道是否有人可以指出我缺少的内容。

我有一个 ComboBox,它绑定到一个简单的 ObservableCollection。此外,我还将 OneWay 绑定中的 SelectedIndex 绑定到某个属性。

在我的应用程序中,我想要清除集合并使用不同的数据重新填充它,并将 SelectedIndex 设置为新值。由于某种原因,SelectedIndex 绑定不起作用。

我附上了问题的一些重现:

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;
    }
}

xaml 是:

<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>

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>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

恰似旧人归 2024-08-25 04:23:57

该问题完全是由 aaaa() 方法中的 Tables.Clear() 行引起的。由于 Tables 是一个可观察集合,因此清除集合的所有内容会导致 WPF 使用新的空列表更新显示。然后它尝试使用 SelectedIndex 选择当前活动的项目,该项目不存在(因为列表现在为空)。结果,绑定引擎留下了一个无法应用的值,并决定停用并分离绑定逻辑:

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

当它到达“TheIndex = j;”时行,绑定不再处于活动状态,并且看不到 TheIndex 的更改,这意味着不再选择所需的索引。

有几种解决方案可以解决此问题:

  1. 不要每次都清除整个集合。在不清除集合的情况下,数据绑定逻辑始终有一个索引可供选择,这意味着它永远不会分离。
  2. 使用TwoWay绑定。这有效是因为现在ComboBox参与了绑定;当您清除 Tables 时,绑定会尝试设置但无法找到索引,因此 ComboBox 会重置为特殊的“无索引”位置 -1,然后写回到 TheIndex (双向部分),这是一个有效值,因此绑定逻辑不会分离。
  3. 清除集合之前不选择任何索引 (-1)。如果清除 Tables 时未选择任何索引 (-1),则 ComboBox 不会尝试apply SelectedItem,这意味着它不会“看到”集合被清空和重新填充,因此不会分离。

    private void aaaa(对象发送者, RoutedEventArgs e)
    {
        索引=-1;
        j = (j + 1)%10;
        表.Clear();
        for (int i = 0; i < 10; i++)
        {
            Tables.Add(i.ToString());
        }
        索引 = j;
    }
    

出于性能、架构和清晰度方面的原因,我强烈推荐选项 1,尽管我意识到您的实际场景可能更复杂并且需要类似于 3 的内容。


旁注:

找出背后的原因当使用像上面发布的那样的绑定跟踪时,这样的绑定问题相当容易。通过声明 System.Diagnostics 命名空间并向导致问题的绑定添加 PresentationTraceSources.TraceLevel=High 来打开它们以进行单个绑定:

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

调试 WPF 绑定的更多方法是 此处

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.

烧了回忆取暖 2024-08-25 04:23:57

我知道这是一个老问题,但我自己刚刚经历过这个问题,所以根据 @Nicholas Armstrong 的答案的选项 1 编写了一个辅助方法,并认为我会分享它,希望有人会发现它有用:

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])));
        }
    }
}

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])));
        }
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文