【发布时间】:2011-08-12 22:17:24
【问题描述】:
我需要将属性绑定到标签。我写了以下代码: 标签的 xml 是
<Label Canvas.Left="807.3" Canvas.Top="148.9" Height="33.567" x:Name="label2"
Width="98" FontFamily="Tw Cen MT" FontSize="24" FontWeight="Bold"
Foreground="#FFFEE3A4"
Content="{Binding Path=UserInformation.AccountBalance,Mode=OneWay}">
<Label.Background>
<ImageBrush />
</Label.Background>
</Label>
拥有 AccountBalance 的类
public class CustomerInformation : INotifyPropertyChanged
{
public CustomerInformation()
{
_Balance = 0.0;
}
#region INotifyPropertyChanged Members
public event PropertyChangedEventHandler PropertyChanged;
#endregion
public double AccountBalance
{
get { return _Balance; }
set
{
_wepaBalance = value;
FirePropertyChanged("AccountBalance");
}
}
protected void FirePropertyChanged(string propertyName)
{
if (this.PropertyChanged != null)
{
this.PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
}
}
}
datacontext 设置如下
this.LayoutRoot.DataContext = this;
在 xaml.cs 后面编写了以下代码来访问作为全局对象的 UserInfo
public CustomerInformation UserInformation
{
get
{
return Globalobjs._Object.UserInfo;
}
}
xamls.cs 仅派生自 Window。
问题是INotifyPropertyChanged的PropertyChangedEventHandler在调用时总是为null。
任何一位可以帮助我解决这个问题吗?
【问题讨论】:
-
你能把src代码也发一下吗?
-
你将
DataContext设置在哪里?您能否还为NotifyChanged实现添加代码? -
当然是 VoodooChild 和 Townsean。感谢您的关注。
标签: c# wpf xaml data-binding