内容简介:代码日志版权声明:翻译自:http://stackoverflow.com/questions/8580477/how-to-bind-local-property-on-control-in-wpf
我有两个WPF控件
<Button HorizontalAlignment="Center"
Name="btnChange"
Click="btnChange_Click"
Content="Click Me" />
<Label Name="lblCompanyId"
HorizontalAlignment="Center"
DataContext="{Binding ElementName=_this}"
Content="{Binding Path=CompanyName}" />
我们可以看到标签绑定到本地属性(在代码后面),当我点击按钮时,我没有看到任何值的标签
以下是我的代码背后…
public static readonly DependencyProperty CompanyNameProperty =
DependencyProperty.Register("CompanyName", typeof(string), typeof(Window3), new UIPropertyMetadata(string.Empty));
public string CompanyName {
get { return (string)this.GetValue(CompanyNameProperty); }
set { this.SetValue(CompanyNameProperty, value); }
}
private void btnChange_Click(object sender, RoutedEventArgs e) {
this.CompanyName = "This is new company from code beind";
}
问候,
尝试
Content="{Binding ElementName=_this, Path=CompanyName}"
没有DataContext绑定
编辑
我的代码没有问题,将你的窗口命名为x:Name =“_ this”?
<Window x:Class="WpfStackOverflowSpielWiese.Window3"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="Window3"
Height="300"
Width="300"
x:Name="_this">
<Grid>
<StackPanel>
<Button HorizontalAlignment="Center"
Name="btnChange"
Click="btnChange_Click"
Content="Click Me" />
<Label Name="lblCompanyId"
HorizontalAlignment="Center"
DataContext="{Binding ElementName=_this}"
Content="{Binding Path=CompanyName}"></Label>
</StackPanel>
</Grid>
</Window>
你的窗口真的是Window3吗?
public partial class Window3 : Window
{
public Window3() {
this.InitializeComponent();
}
public static readonly DependencyProperty CompanyNameProperty =
DependencyProperty.Register("CompanyName", typeof(string), typeof(Window3), new UIPropertyMetadata(string.Empty));
public string CompanyName {
get { return (string)this.GetValue(CompanyNameProperty); }
set { this.SetValue(CompanyNameProperty, value); }
}
private void btnChange_Click(object sender, RoutedEventArgs e) {
this.CompanyName = "This is new company from code beind";
}
}
希望有所帮助
代码日志版权声明:
翻译自:http://stackoverflow.com/questions/8580477/how-to-bind-local-property-on-control-in-wpf
以上就是本文的全部内容,希望本文的内容对大家的学习或者工作能带来一定的帮助,也希望大家多多支持 码农网
猜你喜欢:- vue 表单控件与绑定(七)
- WindowsXamlHost:在 WPF 中使用 UWP 控件库中的控件
- WindowsXamlHost:在 WPF 中使用 UWP 控件库中的控件
- Golang Echo数据绑定中time.Time类型绑定失败
- Flutter控件--Scaffold
- Flutter控件--AppBar
本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们。
ODPS权威指南
李妹芳 / 人民邮电出版社 / 2014-12 / 69元
ODPS(Open Data Processing Service)是阿里巴巴自主研发的海量数据处理和分析的服务平台,主要应用于数据分析、海量数据统计、数据挖掘、机器学习和商业智能等领域。目前,ODPS不仅在阿里内部得到广泛应用,享有很好的口碑,正逐步走向第三方开放市场。 本书是学习和掌握ODPS的权威指南,作者来自阿里ODPS团队。全书共13章,主要内容包括:ODPS入门、整体架构、数据通......一起来看看 《ODPS权威指南》 这本书的介绍吧!