我有兩個控件在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";
}
問候,
謝謝punker76。你的解決方案工作我只需要添加x:Name =「_ this」和boooom。 – usergaro 2011-12-21 18:27:02