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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8580477/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 23:13:19  来源:igfitidea点击:

How to bind local property on control in WPF

wpfbindingpropertieslocal

提问by usergaro

I have two controls on 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}" />

As we can see that label is bound to local property(in code Behind), I don't see any value on label when I click button...

正如我们所看到的,标签绑定到本地属性(在后面的代码中),当我单击按钮时,我在标签上看不到任何值...

Below is my code 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";
}

Regards,

问候,

回答by punker76

try

尝试

Content="{Binding ElementName=_this, Path=CompanyName}"

without the DataContextbinding

没有DataContext绑定

EDIT

编辑

i have no problems with your code, have named your window to x:Name="_this"?

我对你的代码没有问题,已经将你的窗口命名为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>

and is your window really Window3?

你的窗户是真的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";
  }
}

hope that helps

希望有帮助

回答by Rachel

You are currently binding your Label's DataContextto a Button, and then trying to set it's Contentto CompanyName, however CompanyNameis not a valid property on Button

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

Specify DataContextin your binding Pathto bind to Button.DataContext.CompanyNameinstead of Button.CompanyName

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

Also, I'd recommend just binding the Content instead of binding both the DataContext and Content

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

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

And if your code looks exactly like the code sample posted, then both the Buttonand Labelhave the same DataContext, so you can bind directly to CompanyName

如果您的代码看起来与发布的代码示例完全一样,那么ButtonLabel都具有相同的DataContext,因此您可以直接绑定到CompanyName

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

Edit

编辑

Just noticed that your Label's binding was to a control named _this. I had assumed it was the Button, although I see now that your Button's name is btnChange, not _this.

刚刚注意到您的 Label 绑定到名为_this. 我原以为它是 Button,虽然我现在看到你的 Button 的名字是btnChange,而不是_this

It doesn't matter though, the answer is still the same. You're trying to bind to a UI Control's CompanyNameproperty, which is not a valid property.

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