Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to bind local property on control in WPF

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"; } 
like image 884
usergaro Avatar asked Dec 20 '11 18:12

usergaro


People also ask

How do you bind property in XAML?

One-Way Data Binding The following XAML code creates four text blocks with some properties. Text properties of two text blocks are set to “Name” and “Title” statically, while the other two text blocks Text properties are bound to “Name” and “Title” which are class variables of Employee class which is shown below.

What is two way binding in WPF?

Two way binding is used when we want to update some controls property when some other related controls property change and when source property change the actual control also updates its property.

What is the use of INotifyPropertyChanged in WPF?

INotifyPropertyChanged interface is used to notify the view or ViewModel that it does not matter which property is binding; it is updated. Let's take an example for understanding this interface. Take one WPF Window in which there are a total of three fields: First Name, Last Name and Full Name.


1 Answers

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";   } } 
like image 196
punker76 Avatar answered Oct 20 '22 09:10

punker76