如何在WPF中绑定控件上的本地属性

use*_*aro 26 wpf binding properties local

我在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}" />
Run Code Online (Sandbox Code Playgroud)

我们可以看到标签绑定到本地属性(在代码Behind中),当我点击按钮时,我在标签上看不到任何值...

下面是我的代码背后......

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";
}
Run Code Online (Sandbox Code Playgroud)

问候,

pun*_*r76 42

尝试

Content="{Binding ElementName=_this, Path=CompanyName}"
Run Code Online (Sandbox Code Playgroud)

没有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>
Run Code Online (Sandbox Code Playgroud)

真的是你的窗户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";
  }
}
Run Code Online (Sandbox Code Playgroud)

希望有所帮助


Rac*_*hel 5

您当前正在将Label的绑定DataContextButton,然后尝试将其设置ContentCompanyName,但是CompanyName在上不是有效的属性Button

DataContext在绑定中指定Path要绑定Button.DataContext.CompanyName而不是Button.CompanyName

另外,我建议仅绑定内容,而不是同时绑定DataContext和Content

<Label Content="{Binding ElementName=btnChange, Path=DataContext.CompanyName}" />
Run Code Online (Sandbox Code Playgroud)

而且,如果您的代码看起来与所发布的代码示例完全相同,则ButtonLabel都相同DataContext,因此您可以直接绑定到CompanyName

<Label Content="{Binding CompanyName}" />
Run Code Online (Sandbox Code Playgroud)

编辑

刚刚注意到,您Label的绑定是绑定到名为的控件_this。我以为是Button,尽管现在我看到您Button的名字btnChange不是_this

没关系,答案还是一样。您正在尝试绑定到UI控件的CompanyName属性,这不是有效的属性。