【问题标题】:How to bind local property on control in WPF如何在 WPF 中的控件上绑定本地属性
【发布时间】:2012-01-24 17:07:21
【问题描述】:

我在 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";
}

【问题讨论】:

    标签: wpf binding properties local


    【解决方案1】:

    试试:

    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" 和 boooom。
    • 快进到 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. - 那么解决方案是什么?
    【解决方案2】:

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

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

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

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

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

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

    编辑

    刚刚注意到您的标签绑定到一个名为_this 的控件。我原以为是 Button,虽然我现在看到您的 Button 的名称是 btnChange,而不是 _this

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

    【讨论】:

    • @punker76 查看我的编辑。 _this 必须引用一些 UI 控件,我错误地认为它引用了 Button,因为它是代码示例的一部分。原则仍然适用,即使控件不是 Button。
    • 在绑定中添加 ElementName ......正是我整个下午一直在寻找的东西!谢谢!
    猜你喜欢
    • 2015-06-03
    • 1970-01-01
    • 2010-12-14
    • 2012-02-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-12-12
    相关资源
    最近更新 更多