I have this Text
dependency property in code behind:
public static DependencyProperty TextProperty =
DependencyProperty.Register("Text", typeof(string), typeof(MainWindow),
new PropertyMetadata("Hello world"));
public string Text {
get { return (string)GetValue(TextProperty); }
set { SetValue(TextProperty, value); }
}
I want to bind content of label to that Text
property so that the label displays actual value of Text
property and vice-versa.
<Label Content="{Binding ???}" />
How can I do it ?
I have done that some time before but now I cannot remember how - and it is very simple. The simplest code will be accepted.
Code-behind is a term used to describe the code that is joined with markup-defined objects, when a XAML page is markup-compiled. This topic describes requirements for code-behind as well as an alternative inline code mechanism for code in XAML.
The binding references the source object. To set the data binding, use the following two members of the target class: The BindingContext property specifies the source object. The SetBinding method specifies the target property and source property.
Data binding is a mechanism in XAML applications that provides a simple and easy way for Windows Runtime Apps using partial classes to display and interact with data. The management of data is entirely separated from the way the data is displayed in this mechanism.
Data binding is a mechanism in WPF applications that provides a simple and easy way for Windows Runtime apps to display and interact with data. In this mechanism, the management of data is entirely separated from the way data. Data binding allows the flow of data between UI elements and data object on user interface.
Set DataContext of your Window/Control to the same class and then specify the path on the binding, something like this:
public class MyWindow : Window {
public MyWindow() {
InitializeComponents();
DataContext = this;
}
public string Text { ... }
}
Then in your xaml:
<Label Content="{Binding Path=Text}">
You have to set the DataContext of the window for it to work. XAML:
<Window x:Class="WpfApplication2.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="350" Width="525"
DataContext="{Binding RelativeSource={RelativeSource Self}}">
<Grid>
<StackPanel>
<Label Content="{Binding Text}" />
<Button Content="Click me" Click="HandleClick" />
</StackPanel>
</Grid>
</Window>
Code-behind:
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
public static DependencyProperty TextProperty = DependencyProperty.Register("Text", typeof(string), typeof(MainWindow), new PropertyMetadata("Hello world"));
public string Text
{
get { return (string)GetValue(TextProperty); }
set { this.SetValue(TextProperty, value); }
}
public MainWindow()
{
InitializeComponent();
}
protected void HandleClick(object sender, RoutedEventArgs e)
{
this.Text = "Hello, World";
}
}
Setting DataContext in XAML to Code-Behind can be a little bit tricky but in general these situation are the most common:
.
<Window
blahhhh..
DataContext={Binding RelativeSource={RelativeSource Mode=Self}}>
or
<UserControl
Blahhhh....
DataContext={Binding RelativeSource={RelativeSource Mode=Self}}>
2. if you set the DataContext of the Window or user control to something else than the code behind and have a child control you would need to set it's DataContext to the Code-Behind you can use the following:
<Label DataContext={Binding RelativeSource={RelativeSource Mode=FindAncestor,AncestorType=Window}}/>
for custom UserControl :
<Label DataContext={Binding RelativeSource={RelativeSource Mode=FindAncestor,AncestorType=UserControl}}/>
in this Case setting the DataContext to self, will make Binding refer to the Label Object itself not the Control's Code-Behind. I hope that will help.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With