c# – WPF INotifyPropertyChanged没有更新数组属性?
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了c# – WPF INotifyPropertyChanged没有更新数组属性?,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含2692字,纯文字阅读大概需要4分钟。
内容图文
![c# – WPF INotifyPropertyChanged没有更新数组属性?](/upload/InfoBanner/zyjiaocheng/757/4ab892dbf13a4d3abd4b907f8b507a0b.jpg)
我创建了一个小例子来演示我遇到的问题.
我的班级第一名:
public class DisplayRow : INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged;
private int?[] values;
private string title;
public string Title
{
get { return title; }
set
{
title = value;
if (PropertyChanged != null) PropertyChanged(this, new PropertyChangedEventArgs("Title"));
}
}
public int?[] Values
{
get { return values; }
set
{
values = value;
if (PropertyChanged != null) PropertyChanged(this, new PropertyChangedEventArgs("Values[]"));
}
}
public DisplayRow()
{
Values = new int?[6];
}
}
问题是Values属性,因为它是一个数组.我不确定当数组中的元素更新时如何正确调用INotifyPropertyChanged.
这是我的xaml:
<Window x:Class="WpfApplication5.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>
<ListBox x:Name="MyListBox" Margin="0,0,0,65">
<ListBox.ItemTemplate>
<DataTemplate>
<WrapPanel>
<TextBlock Text="{Binding Path=Title}" />
<TextBlock Text="{Binding Path=Values[0]}" Margin="5,0,0,0" />
<TextBlock Text="{Binding Path=Values[1]}" Margin="5,0,0,0" />
<TextBlock Text="{Binding Path=Values[2]}" Margin="5,0,0,0" />
<TextBlock Text="{Binding Path=Values[3]}" Margin="5,0,0,0" />
<TextBlock Text="{Binding Path=Values[4]}" Margin="5,0,0,0" />
<TextBlock Text="{Binding Path=Values[5]}" Margin="5,0,0,0" />
</WrapPanel>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
<Button Height="23" Margin="27,0,0,23" Name="button1" VerticalAlignment="Bottom" HorizontalAlignment="Left" Width="74" Click="button1_Click">Button</Button>
</Grid>
</Window>
而背后的代码:
public partial class Window1 : Window
{
private readonly ObservableCollection<DisplayRow> displayRows = new ObservableCollection<DisplayRow>();
public Window1()
{
InitializeComponent();
displayRows.Add(new DisplayRow {Title = "Item 1", Values = new int?[] {1, 2, 3, 4, 5, 6}});
displayRows.Add(new DisplayRow {Title = "Item 2", Values = new int?[] {7, 8, 9, 10, 11, 12}});
displayRows.Add(new DisplayRow {Title = "Item 3", Values = new int?[] {13, 14, 15, 16, 17, 18}});
MyListBox.ItemsSource = displayRows;
}
private void button1_Click(object sender, RoutedEventArgs e)
{
foreach (DisplayRow row in displayRows)
{
row.Values[0] = 99;
}
}
}
当我单击按钮时,它会更改第一行的值,但该更改不会反映在UI上.如果我更改标题属性,标题将正确更新.
任何想法如何我可以调用INotifyPropertyChanged,以便它了解数组元素已更新?
解决方法:
现有代码不起作用的原因是因为您正在修改数组中的值,而不是将整个数组本身重新分配给属性.因此,您不会触发属性更改事件.
我根本不会使用数组,而是使用ObservableCollection来实现WPF绑定基础结构挂钩的INotifyCollectionChanged,以更好地理解集合本身的变化.
内容总结
以上是互联网集市为您收集整理的c# – WPF INotifyPropertyChanged没有更新数组属性?全部内容,希望文章能够帮你解决c# – WPF INotifyPropertyChanged没有更新数组属性?所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。