c# - Itemsource 未更新 (wpf,c#)
问题描述
我在读取更改后的值时遇到问题:
XAML 中的绑定:
Text="{Binding Path=Infotext,Mode=TwoWay}"
我尝试通过以下方式读取一个值(它绑定到一个文本框,类是票证,ic_Tickets 是一个带有“票证”的 Itemscontrol):
string text=((ObservableCollection<Ticket>)ic_Tickets.ItemsSource).Where(Ticket => Ticket.id == ((MenuItem)sender).Tag.ToString()).First().Infotext;
有了这个(在公共类 Ticket:INotifyPropertyChanged 我得到了旧的绑定值
public string Infotext { get
{
return Infotext_int;
}
set {
Infotext_int = value;
NotifyPropertyChanged();
} }
但如果我添加这样的消息框:
public string Infotext { get
{
return Infotext_int;
}
set {
Infotext_int = value;
MessageBox.Show("!!!"); //or inside the NotifyPropertyChanged()
NotifyPropertyChanged();
} }
我得到了新的价值......(我想要新的价值,但它不可能总是显示一个消息框)
因此,只有当消息框显示 oO 时,它才会将新值设置为 Itemssource
通知功能:
private void NotifyPropertyChanged(String propertyName = "")
{
if (PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
}
}
解决方案
推荐阅读
- webview - 在 VStack 中包装 WebView 时出错:无法推断复杂的闭包返回类型;添加显式类型以消除歧义
- javascript - 如何使用 Javascript 中的函数来构建交互式文本?
- javascript - MVC JS deletethisproduct 未在 HTMLAnchorElement.onclick 中定义(VM457 索引:40)
- angular - 如何在@angular/google-maps infoWindow 中显示动态内容
- function - 基于 Actor 的模型相关的多个问题
- asp.net - 使用 GraphServiceClient 检索 Azure 广告成员的组信息
- azure-web-app-service - 允许 Azure Web App 访问 VM 上的数据库,而无需将该 VM 暴露给外界
- css - 如何在一个 flexbox 中上下对齐项目?
- python - Python Capn Proto:恢复者?
- kubernetes - 使用补丁从节点中删除标签