如何强制ListBox重新加载ListBoxItems的属性 [英] How to force ListBox to reload properties of ListBoxItems
问题描述
我有一个ObservableCollection对象 ListBox
item source,所以每当我添加/删除项目到集合中时,ListBox会相应的更改,但是当我更改集合中某些对象的属性时,ListBox仍会呈现旧值。
即使我修改一些属性,然后添加/删除对象到集合,没有任何反应,我仍然看到旧的值。
还有其他方法做这个?我发现接口INotifyPropertyChanged,但我不知道如何使用它。
我同意Matt的评论。以下是一小段代码,以显示如何实现INotifyPropertyChanged。
===========
代码隐藏
===========
using System;
使用System.Collections.Generic;
使用System.Linq;
使用System.Text;
使用System.Windows;
使用System.Windows.Data;
使用System.Windows.Documents;
命名空间WpfApplication1
{
///< summary>
/// Window1.xaml的交互逻辑
///< / summary>
public partial class Window1:Window
{
昵称名称;
public Window1()
{
InitializeComponent();
this.addButton.Click + = addButton_Click;
this.names = new Nicknames();
dockPanel.DataContext = this.names;
}
void addButton_Click(object sender,RoutedEventArgs e)
{
this.names.Add(new Nickname(myName.Text,myNick.Text));
}
}
public class昵称:System.Collections.ObjectModel.ObservableCollection< Nickname> {}
public class昵称:System.ComponentModel.INotifyPropertyChanged
{
public event System.ComponentModel.PropertyChangedEventHandler PropertyChanged;
void Notify(string propName)
{
if(PropertyChanged!= null)
{
PropertyChanged(this,new System.ComponentModel.PropertyChangedEventArgs(propName));
}
}
string name;
public string Name
{
get {return name; }
set
{
name = value;
通知(名称);
}
}
string nick;
public string Nick
{
get {return nick; }
set
{
nick = value;
通知(Nick);
}
}
public昵称():this(name,nick){}
public昵称(字符串名称,字符串nick)
{
this.name = name;
this.nick = nick;
}
public override string ToString()
{
return Name.ToString()++ Nick.ToString();
}
}
}
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 =Window1Height =300Width = 300>
< Grid>
< DockPanel x:Name =dockPanel>
< TextBlock DockPanel.Dock =Top>
< TextBlock VerticalAlignment =Center> Name:< / TextBlock>
< TextBox Text ={Binding Path = Name}Name =myName/>
< TextBlock VerticalAlignment =Center> Nick:< / TextBlock>
< TextBox Text ={Binding Path = Nick}Name =myNick/>
< / TextBlock>
< Button DockPanel.Dock =Bottomx:Name =addButton>添加< / Button>
< ListBox ItemsSource ={Binding}IsSynchronizedWithCurrentItem =True/>
< / DockPanel>
< / Grid>
Is there any way, how to force ObservableCollection to fire CollectionChanged?
I have a ObservableCollection of objects ListBox
item source, so every time I add/remove item to collection, ListBox changes accordingly, but when I change properties of some objects in collection, ListBox still renders the old values.
Even if I do modify some properties and then add/remove object to the collection, nothing happens, I still see old values.
Is there any other way around to do this? I found interface INotifyPropertyChanged, but I don't know how to use it.
I agree with Matt's comments above. Here's a small piece of code to show how to implement the INotifyPropertyChanged.
===========
Code-behind
===========
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Data;
using System.Windows.Documents;
namespace WpfApplication1
{
/// <summary>
/// Interaction logic for Window1.xaml
/// </summary>
public partial class Window1 : Window
{
Nicknames names;
public Window1()
{
InitializeComponent();
this.addButton.Click += addButton_Click;
this.names = new Nicknames();
dockPanel.DataContext = this.names;
}
void addButton_Click(object sender, RoutedEventArgs e)
{
this.names.Add(new Nickname(myName.Text, myNick.Text));
}
}
public class Nicknames : System.Collections.ObjectModel.ObservableCollection<Nickname> { }
public class Nickname : System.ComponentModel.INotifyPropertyChanged
{
public event System.ComponentModel.PropertyChangedEventHandler PropertyChanged;
void Notify(string propName)
{
if (PropertyChanged != null)
{
PropertyChanged(this, new System.ComponentModel.PropertyChangedEventArgs(propName));
}
}
string name;
public string Name
{
get { return name; }
set
{
name = value;
Notify("Name");
}
}
string nick;
public string Nick
{
get { return nick; }
set
{
nick = value;
Notify("Nick");
}
}
public Nickname() : this("name", "nick") { }
public Nickname(string name, string nick)
{
this.name = name;
this.nick = nick;
}
public override string ToString()
{
return Name.ToString() + " " + Nick.ToString();
}
}
}
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>
<DockPanel x:Name="dockPanel">
<TextBlock DockPanel.Dock="Top">
<TextBlock VerticalAlignment="Center">Name: </TextBlock>
<TextBox Text="{Binding Path=Name}" Name="myName" />
<TextBlock VerticalAlignment="Center">Nick: </TextBlock>
<TextBox Text="{Binding Path=Nick}" Name="myNick" />
</TextBlock>
<Button DockPanel.Dock="Bottom" x:Name="addButton">Add</Button>
<ListBox ItemsSource="{Binding}" IsSynchronizedWithCurrentItem="True" />
</DockPanel>
</Grid>
这篇关于如何强制ListBox重新加载ListBoxItems的属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!