How to bind local property on control in WPF

87,681

Solution 1

Try:

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

Without the DataContext binding.

EDIT

I have no problems with your code, have named your window to 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>

Is your window really 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";
  }
}

Solution 2

You are currently binding your Label's DataContext to a Button, and then trying to set it's Content to CompanyName, however CompanyName is not a valid property on Button

Specify DataContext in your binding Path to bind to Button.DataContext.CompanyName instead of Button.CompanyName

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

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

And if your code looks exactly like the code sample posted, then both the Button and Label have the same DataContext, so you can bind directly to 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.

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

Share:
87,681
usergaro
Author by

usergaro

Updated on December 28, 2020

Comments

  • usergaro
    usergaro over 3 years

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