[英]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.