wpf INotifyPropertyChanged.PropertyChanged 已触发,但表单加载后 UI 未更新
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14927240/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
INotifyPropertyChanged.PropertyChanged fired, but UI not updated after form loads
提问by mike01010
Below is an example of my model , ViewModeland xaml binding. The viewmodel implements INotifyPropertChanged. The problem i'm having is...when the wpf form first loads i set ActiveStockand i see both setter and getter being called and the ui is updated to reflect the data correctly.
However, when i later set StockViewModel.ActiveStock, FirePropertyChangedis invoked but i don't see the getter being called, and consequently the UI does not update to reflect the new data. Any ideas what might be happening here?
下面是我的模型ViewModel和 xaml 绑定的示例。视图模型实现INotifyPropertChanged. 我遇到的问题是...当 wpf 表单第一次加载我设置时ActiveStock,我看到 setter 和 getter 都被调用,并且更新了 ui 以正确反映数据。
但是,当我稍后设置StockViewModel.ActiveStock 时,FirePropertyChanged被调用但我没有看到正在调用 getter,因此 UI 不会更新以反映新数据。任何想法这里可能会发生什么?
The second question i have is whether i also need to raise PropertyChangedfor the child properties (PriceDataand CompanyData) of my model when ViewModel.ActiveStockis changed?
我的第二个问题是我是否还需要在更改时为模型PropertyChanged的子属性(PriceData和CompanyData)提高ViewModel.ActiveStock?
public class Stock
{
public string Ticker { get; set; }
public StockData PriceData { get; set; }
public StockData CompanyData { get; set; }
}
public class StockData
{
...
}
public class StockViewModel:INotifyPropertyChanged
{
private Stock _activeStock;
public Stock ActiveStock
{
get{ return _activeStock;}
set{ _activeStock = value; FirePropertyChanged("ActiveStock");}
}
...
}
XAML:
XAML:
<UserControl Template="{StaticResource StockTemplate}" DataContext="{Binding ActiveStock}" Tag="{Binding PriceData}" />
<UserControl Template="{StaticResource StockTemplate}" DataContext="{Binding ActiveStock}" Tag="{Binding CompanyData}" />
Edit: if i remove the DataContext binding for the UserControl and instead set the DataContext for these two controls in code behind when ActiveStock changes, it works fine. why???
编辑:如果我删除 UserControl 的 DataContext 绑定,而是在 ActiveStock 更改时在后面的代码中为这两个控件设置 DataContext,它工作正常。为什么???
回答by sa_ddam213
The getter is not being called because as far as I can see nothing is "getting" the value, The only properties used are PriceDataand CompanyDataand these don't use INotifyPropertyChanged
吸气剂不会被调用因为据我可以看到什么是“让”的值,使用的唯一属性PriceData,并CompanyData和这些不使用INotifyPropertyChanged
You will have to implement INotifyPropertyChangedon your Stockclass for the UIto reflect the changes.
您必须INotifyPropertyChanged在您的Stock班级上实施UI以反映更改。
public class Stock : INotifyPropertyChanged
{
private string _ticker;
private StockData _priceData;
private StockData _companyData;
public string Ticker
{
get { return _ticker; }
set { _ticker = value; NotifyPropertyChanged("Ticker"); }
}
public StockData PriceData
{
get { return _priceData; }
set { _priceData = value; NotifyPropertyChanged("PriceData"); }
}
public StockData CompanyData
{
get { return _companyData; }
set { _companyData = value; NotifyPropertyChanged("CompanyData"); }
}
public event PropertyChangedEventHandler PropertyChanged;
private void NotifyPropertyChanged(string property)
{
if (PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs(property));
}
}
}
回答by Sijmen Koffeman
You might want to try to specify the mode property on your datacontext bindings.
您可能想尝试在数据上下文绑定上指定 mode 属性。
DataContext="{Binding ActiveStock, Mode=OneWay}"
I'm not sure that OneTime is the default binding for DataContext, but it would explain so if the above helps.
我不确定 OneTime 是 DataContext 的默认绑定,但如果上述内容有帮助,它会解释。
The second question has been answered by sa_ddam213.
第二个问题已由 sa_ddam213 回答。
HTH
HTH

