清除可观察集合后 ComboBox SelectedItem 不会更改
我遇到了与 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
该问题完全是由
aaaa()
方法中的Tables.Clear()
行引起的。由于Tables
是一个可观察集合,因此清除集合的所有内容会导致 WPF 使用新的空列表更新显示。然后它尝试使用SelectedIndex
选择当前活动的项目,该项目不存在(因为列表现在为空)。结果,绑定引擎留下了一个无法应用的值,并决定停用并分离绑定逻辑:当它到达“TheIndex = j;”时行,绑定不再处于活动状态,并且看不到 TheIndex 的更改,这意味着不再选择所需的索引。
有几种解决方案可以解决此问题:
TwoWay
绑定。这有效是因为现在ComboBox参与了绑定;当您清除Tables
时,绑定会尝试设置但无法找到索引,因此 ComboBox 会重置为特殊的“无索引”位置 -1,然后写回到TheIndex (双向部分),这是一个有效值,因此绑定逻辑不会分离。
清除集合之前不选择任何索引 (-1)。如果清除
Tables
时未选择任何索引 (-1),则 ComboBox 不会尝试applySelectedItem
,这意味着它不会“看到”集合被清空和重新填充,因此不会分离。出于性能、架构和清晰度方面的原因,我强烈推荐选项 1,尽管我意识到您的实际场景可能更复杂并且需要类似于 3 的内容。
旁注:
找出背后的原因当使用像上面发布的那样的绑定跟踪时,这样的绑定问题相当容易。通过声明 System.Diagnostics 命名空间并向导致问题的绑定添加
PresentationTraceSources.TraceLevel=High
来打开它们以进行单个绑定:调试 WPF 绑定的更多方法是 此处。
The problem is entirely caused by the
Tables.Clear()
line in youraaaa()
method. SinceTables
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 usingSelectedIndex
, 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: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:
TwoWay
binding. This works because now the ComboBox participates in binding; you clearTables
, 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 toTheIndex
(the two-way part), which is a valid value so the binding logic doesn't detach.Select no index (-1) before clearing the collection. If no index (-1) is selected when
Tables
is cleared, then ComboBox doesn't try to applySelectedItem
, which means it doesn't 'see' the collection emptied and re-filled, and therefore, doesn't detach.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=High
to the binding that is causing trouble:More ways of debugging WPF bindings are here.
我知道这是一个老问题,但我自己刚刚经历过这个问题,所以根据 @Nicholas Armstrong 的答案的选项 1 编写了一个辅助方法,并认为我会分享它,希望有人会发现它有用:
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: