[英]Update/Refresh TextBlock which is bind to another element property in wpf/xaml
我想更新绑定到listview项目属性的textblock中的文本。 这是我将文本块绑定到listview项目的方式。
mWindow.xaml
<ListView Name="ListViewDetails"
ItemsSource="{Binding Persons}"
SelectedItem="{Binding CurrentPerson}">
...
</ListView>
<TextBlock>
<Run Text="{Binding ElementName=ListViewDetails, Path=SelectedItem.Office}"/>
...
</TextBlock>
如果listview中的item属性更改,则不会更新文本。
mWindow.xaml.cs
public partial class mWindow: Window , INotifyPropertyChanged
{
private Person currentPerson;
public Person CurrentPerson
{
get
{
return currentPerson;
}
set
{
this.currentPerson = value;
this.NotifyPropertyChanged("CurrentPerson");
}
}
public event PropertyChangedEventHandler PropertyChanged;
private void NotifyPropertyChanged(string propertyName)
{
var handler = this.PropertyChanged;
if (handler != null)
{
handler(this, new PropertyChangedEventArgs(propertyName));
}
}
private void editLisView{
...
// refresh ListView
ICollectionView view =CollectionViewSource.GetDefaultView(ListViewInsuranceDetails.ItemsSource);
view.Refresh();
}
}
我以为我必须为CurrentPerson属性实现INotifyPropertyChanged。 当我为人员类实现INotifyPropertyChanged时,它可以工作。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.