将控件绑定到该对象的代码隐藏属性
我想将 Label 控件绑定到代码隐藏类中有一个属性:
public MainWindow()
{
InitializeComponent();
this.Label1Content = "some text";
}
public string Label1Content { get; set; }
但绑定失败。显然我在绑定配置中遗漏了一些东西,但我不知道是什么。我知道如何使用 C# 绑定此属性,但如何使用 XAML 绑定它而不声明 DataContext?
I have a property in a code-behind class to which I want to bind my Label control:
public MainWindow()
{
InitializeComponent();
this.Label1Content = "some text";
}
public string Label1Content { get; set; }
But the binding fails. Obviously I am missing something in the binding configuration, but I don't know what. I know how to bind this property using C#, but how do I bind it using XAML and without declaring DataContext?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果您不想在任何地方声明数据上下文,您可以使用
If you don't want to declare a datacontext anywhere, you could use
即使它是同一个控件,您仍然必须声明
DataContext
:此外,该控件必须实现
INotifyPropertyChanged
,以便您可以引发PropertyChanged
代码>事件。你的财产应该是独立的,如下所示:You still have to declare a
DataContext
, even if it is the same control:Also, the control will have to implement
INotifyPropertyChanged
so you can raise thePropertyChanged
event. You're property should be self-contained like so: