C# 如何在wpf中为标签设置绑定
我需要将属性绑定到标签。我编写了以下代码: 标签的xaml是C# 如何在wpf中为标签设置绑定,c#,wpf,xaml,data-binding,C#,Wpf,Xaml,Data Binding,我需要将属性绑定到标签。我编写了以下代码: 标签的xaml是 <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=UserI
<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>
datacontext设置如下
this.LayoutRoot.DataContext=this
在xaml.cs后面,编写以下代码以访问作为全局对象的UserInfo
public CustomerInformation UserInformation
{
get
{
return Globalobjs._Object.UserInfo;
}
}
xamls.cs仅从窗口派生
问题是INotifyPropertyChanged的PropertyChangedEventHandler在调用时始终为null
在这个问题上,任何1都可以帮助我吗?您要绑定以实现InotifyProperty的datacontext是否已更改 如果这不是MVVM模式的项目,请确保包含要绑定到的属性的类实现该接口,并确保在更改属性时调用事件的委托(例如OnPropertyChanged(“MyProperty”)) 如果它是一个MVVM项目,并且您没有使用框架,那么最好从实现INotifyPropertyChanged的ViewModel库派生所有ViewModel
this.LayoutRoot.DataContext = this;
这是窗口
,但是您正在将窗口
实例设置为DataContext
。将DataContext
设置为UserInformation
this.LayoutRoot.DataContext = Globalobjs._Object.UserInfo;
您正在绑定到Windows的DataContext。但是Windows DataContext与Windows的代码隐藏不同,在这里定义了UserInformation属性。要访问在窗口的代码隐藏中定义的属性,必须设置窗口的Name属性,然后使用以下绑定:
Content="{Binding ElementName=YourWindowName, Path=UserInformation.AccountBalance,Mode=OneWay}"
你也可以发布src代码吗?你在哪里设置你的
DataContext
?请您也为NotifyChanged
实现添加代码好吗?当然可以,VoodooChild和townsean。感谢您的关注。Thank.AccountBalance是实现INotifyPropertyChanged的全局对象的成员。我正在用包含AccountBalance的类的代码更新帖子。我还有其他控件绑定到窗口的不同属性。@logeeks将LayoutRoot.DataContext设置为LayoutRoot的父项没有意义;你知道这一点,不是吗?
Content="{Binding ElementName=YourWindowName, Path=UserInformation.AccountBalance,Mode=OneWay}"