我如何获得NotifyCollectionChangedEventHandler以在WPF绑定中的SortedList的值上工作 [英] How can I get NotifyCollectionChangedEventHandler to work on the Values of a SortedList in WPF binding
问题描述
我有以下两个组合框.当SortedList更改时,一个更新内容,而另一个则不更改.
I''ve got the following two combo boxes. One updates the content when the SortedList changes and the other doesn''t.
<ComboBox Name="cbxNotUpdating"
ItemsSource="{Binding Path=DataContext.Versions.Values, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type Window}}}"
SelectedValue="{Binding Path=Version, Mode=TwoWay}">
</ComboBox>
<ComboBox Name="cbxUpdating"
ItemsSource="{Binding Path=DataContext.Versions, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type Window}}}"
SelectedValue="{Binding Path=Version, Mode=TwoWay}">
</ComboBox>
明显的区别是cbxNotUpdating绑定到值,而cbxUpdating绑定到SortedList本身.
我的列表实现如下所示:
The obvious difference is that cbxNotUpdating binds to the Values and cbxUpdating binds to the SortedList itself.
My List implementation looks like this:
public class VersionList : SortedList<string, Version>, INotifyCollectionChanged
{
...
public event NotifyCollectionChangedEventHandler CollectionChanged;
protected virtual void OnCollectionChanged(NotifyCollectionChangedEventArgs e)
{
if (CollectionChanged != null)
{
CollectionChanged(this, e);
}
}
}
如何进行cbxNotUpdating更新?
我有一个要作为SelectedValue拥有的版本"属性,同时我有一个仅适用于集合的Value部分的模板.
我正在使用vs2010.
How can I make the cbxNotUpdating update?
I''ve got a property that is a ''Version'' that I want to have as the SelectedValue at the same time I have a template that works on just the Value part of the collection.
I''m using vs2010.
推荐答案
感谢SteveAdey指出了致命缺陷.
最后,我绑定到SortedList更改了SelectedValuePath,并使用了与Value绑定的DataTemplate来达到我的期望目标.
我的解决方案如下所示:
Thanks to SteveAdey for pointing out the fatal flaw.
In the end I bound to the SortedList changed the SelectedValuePath and used a DataTemplate binding to the Value to get to my desired goal.
My solution looks like this:
<ComboBox Name="cbxFolder" Grid.Column="1" Grid.Row="0"
ItemsSource="{Binding Path=DataContext.Versions, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type Window}}}"
SelectedValue="{Binding Path=Version, Mode=TwoWay}"
SelectedValuePath="Value"
ComboBox.ItemTemplate="{StaticResource VersionCbxTemplate}">
</ComboBox>
在资源中有了这个DataTemplate
With this DataTemplate in the resources
<DataTemplate x:Key="saturnVersioncbx">
<StackPanel Margin="5" DataContext="{Binding Value}" >...
</StackPanel>
</DataTemplate>
这篇关于我如何获得NotifyCollectionChangedEventHandler以在WPF绑定中的SortedList的值上工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!