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

34

我在 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";
}
2个回答

48

尝试:

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 behind";
  }
}

谢谢 punker76。你的解决方案起作用了!我只需要添加 x:Name="_this" 就可以了。 - usergaro
1
快进到2021年,如果您尝试这样做,您会收到以下错误:MyView.xaml(15,3): error MC3054: The type 'MyView' cannot have a Name attribute. Value types and types without a default constructor can be used as items within a ResourceDictionary.- 那么解决方案是什么呢? - Mike Nakis

9

您目前将标签的DataContext绑定到一个Button,然后尝试将其Content设置为CompanyName,但是CompanyName不是Button上的有效属性。

请在绑定的Path中指定DataContext,以绑定到Button.DataContext.CompanyName而不是Button.CompanyName

此外,我建议只绑定Content而不是同时绑定DataContextContent

<Label Content="{Binding ElementName=btnChange, Path=DataContext.CompanyName}" />

如果您的代码与发布的代码示例完全相同,则 Button Label 具有相同的 DataContext ,因此您可以直接绑定到 CompanyName

<Label Content="{Binding CompanyName}" />

编辑

刚刚注意到你的标签绑定到了一个名为_this的控件上。我原以为它是按钮,但现在我看到你的按钮名称是btnChange而不是_this

不过这并不重要,答案仍然是一样的。你试图绑定到一个UI控件的CompanyName属性,但这不是一个有效的属性。


@punker76 请看我的修改。_this 必须指向某个 UI 控件,而我错误地认为它指的是 Button,因为它是代码示例的一部分。即使控件不是 Button,原则仍然适用。 - Rachel
在绑定中添加ElementName...这正是我整个下午一直在寻找的!谢谢! - thehelix

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接