34

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";
}
GEOCHET
  • 21,119
  • 15
  • 74
  • 98
usergaro
  • 417
  • 1
  • 6
  • 12

2 Answers2

48

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";
  }
}
GEOCHET
  • 21,119
  • 15
  • 74
  • 98
punker76
  • 14,326
  • 5
  • 58
  • 96
  • Thanks punker76. Your solution worked! I had just needed to add x:Name="_this" and boooom. – usergaro Dec 21 '11 at 18:27
  • 1
    Fast forward to 2021, and if you try doing this, you get the following error: `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.` - What is the solution then? – Mike Nakis Apr 10 '21 at 17:06
8

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.

Rachel
  • 130,264
  • 66
  • 304
  • 490
  • @punker76 See my edit. `_this` has to refer to some UI control, and I mistakenly assumed it referred to the Button since it was part of the code sample. The principal still applies, even if it the control isn't a Button. – Rachel Dec 20 '11 at 19:27
  • Adding the ElementName in the Binding...just what I've been looking for all afternoon! Thanks! – thehelix Dec 08 '17 at 02:01