【发布时间】:2014-11-18 20:22:08
【问题描述】:
这一定很明显,但是谁能告诉我为什么我标签中的值只更新一次。我的 PropertyChangedEventHandler 永远不会触发:
<Page.Resources>
x:Key="SoSummaryViewModelDataSource"/>
</Page.Resources>
<Grid DataContext="{StaticResource SoSummaryViewModelDataSource}">
<Label Grid.Row="2"
Margin="30, 0, 0, 0"
FontWeight="Medium"
Content="{Binding TotalDisplayedCustomers, Mode=OneWay, UpdateSourceTrigger=PropertyChanged}"/>
</Grid>
这是我的财产:
public string TotalDisplayedCustomers
{
get { return _totalDisplayedCustomers; }
set
{
if (_totalDisplayedCustomers != value)
{
_totalDisplayedCustomers = value;
OnPropertyChanged("TotalDisplayedCustomers");
}
}
}
这是我的 OnPropertyChanged:
protected void OnPropertyChanged(string propertyName)
{
//when propertyName is TotalDisplayedCustomers, handler is null, why??
PropertyChangedEventHandler handler = PropertyChanged;
if (handler != null)
{
handler(this, new PropertyChangedEventArgs(propertyName));
}
}
【问题讨论】:
-
你尝试过简单的谷歌搜索吗?也看看这个例子stackoverflow.com/questions/17233651/…
-
@user2348184 你如何更改
TotalDisplayedCustomers以便PropertyChangedEventHandler 永远不会触发? -
@DJKRAZE 是的,我有一个吸气剂,所以不确定你的样本有什么用途,你能详细说明一下吗?
-
@user2348184 - 最有可能的实例不同。您确定只在实例
SoSummaryViewModelDataSource上设置值吗? -
@user2348184 加载 ObservableCollection 后,我将 TotalDisplayedCustomers 设置为这样,TotalDisplayedCustomers = string.Format("Customers: {0}", _summaryLineItems.Count);
标签: c# wpf xaml dependency-properties