我是 WPF 数据绑定(bind)的初学者,我刚刚发现了一种令人困惑的行为。也许有人可以帮助我:
我的 xaml 代码将列表框绑定(bind)到列表:
<ListBox ItemsSource="{Binding Path=Axes}" SelectedItem="{Binding Path = SelectedAxis}" DisplayMemberPath = "Name" Name="listboxAxes"/>
如果绑定(bind)的实际项目是一个列表,一切都按预期工作。
但是当我更改为 KeyedCollection 时出现更新问题。调用 NotifyPropertychanged("Axes") 不会更新整个 ListBox。
对此有什么想法吗?
最佳答案
如果 Axes 正在收集的不是具有自己的 Name 属性的类,那么 DisplayMemberPath="Name"属性可能会导致您看不到任何内容。
虽然使用 KeyedCollection 作为 ItemsSource 非常好。 ItemsSource 属性是 ItemsControl.ItemsSource属性(property)。它只需要它所绑定(bind)的实现 IEnumerable。
KeyedCollection确实实现了 IEnumerable,因为它实现了 Collection。
这是一个使用 KeyedCollection 的简短示例:
<Grid>
<DockPanel x:Name="QuickListButtonsStackPanel">
<Button DockPanel.Dock="Top"
Content="Load Animals"
Click="AnimalButtonClick" />
<Button DockPanel.Dock="Top"
Content="Load Objects"
Click="ObjectButtonClick" />
<TextBlock DockPanel.Dock="Bottom"
Background="Azure"
Text="{Binding SelectedExample}" />
<ListBox x:Name="uiListBox"
ItemsSource="{Binding Examples}"
SelectedItem="{Binding SelectedExample}" />
</DockPanel>
</Grid>
以及我们的 Window 和 KeyedCollection 的代码:
public partial class Window1 : Window, INotifyPropertyChanged
{
public Window1()
{
InitializeComponent();
this.DataContext = this;
}
public KeyedCollection<char, string> Examples
{
get;
set;
}
private string mySelectedExample;
public string SelectedExample
{
get
{ return this.mySelectedExample; }
set
{
this.mySelectedExample = value;
this.NotifyPropertyChanged("SelectedExample");
}
}
private void AnimalButtonClick(object sender, RoutedEventArgs e)
{
Examples = new AlphabetExampleCollection();
Examples.Add("Ardvark");
Examples.Add("Bat");
Examples.Add("Cat");
Examples.Add("Dingo");
Examples.Add("Emu");
NotifyPropertyChanged("Examples");
}
private void ObjectButtonClick(object sender, RoutedEventArgs e)
{
Examples = new AlphabetExampleCollection();
Examples.Add("Apple");
Examples.Add("Ball");
Examples.Add("Chair");
Examples.Add("Desk");
Examples.Add("Eee PC");
NotifyPropertyChanged("Examples");
}
#region INotifyPropertyChanged Members
private void NotifyPropertyChanged(String info)
{
if (PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs(info));
}
}
public event PropertyChangedEventHandler PropertyChanged;
#endregion
}
public class AlphabetExampleCollection : KeyedCollection<char, string>
{
public AlphabetExampleCollection() : base() { }
protected override char GetKeyForItem(string item)
{
return Char.ToUpper(item[0]);
}
}
另一个可能发生的问题是,如果您只是从集合中添加/删除项目,而不是重新设置集合。在上面的示例中,您可以看到我们正在重新设置键控集合。如果我们不这样做,那么仅调用 NotifyPropertyChanged 将不会执行任何操作。
让我们再添加两个按钮来演示这一点:
<Button DockPanel.Dock="Top"
Content="Add Zebra"
Click="AddZebraClick" />
<Button DockPanel.Dock="Top"
Content="Add YoYo"
Click="AddYoYoClick" />
还有处理者:
private void AddZebraClick(object sender, RoutedEventArgs e)
{
Examples.Add("Zebra");
NotifyPropertyChanged("Examples");
}
private void AddYoYoClick(object sender, RoutedEventArgs e)
{
Examples.Add("YoYo");
NotifyPropertyChanged("Examples");
}
这两种方法都不会起作用。当您调用 NotifyPropertyChanged 时,必须实际更改该属性。在这种情况下,事实并非如此。我们修改了它的项目,但 Examples 集合仍然是同一个集合。为了解决这个问题,我们可以像在第一部分中那样循环收集,或者如果我们可以访问 UI,我们可以调用:
uiListBox.Items.Refresh();
我们还可以循环 DataSource,我们可以循环 ListBox 的 ItemsSource,或者我们可以清除并重新分配绑定(bind),但仅调用 UpdateTarget() 不会这样做。
关于c# - 数据绑定(bind) wpf KeyedCollection,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/943952/