Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Databind from XAML to code behind

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.

like image 419
Rasto Avatar asked Apr 28 '11 20:04

Rasto


People also ask

What is code behind in WPF?

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.

How do you bind in code behind xamarin forms?

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.

How do you bind in XAML?

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.

Which mechanism connects data in the code with the WPF elements?

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.


3 Answers

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}">
like image 155
Hadi Eskandari Avatar answered Oct 19 '22 00:10

Hadi Eskandari


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";
    }
}
like image 13
Ari Roth Avatar answered Oct 19 '22 00:10

Ari Roth


Setting DataContext in XAML to Code-Behind can be a little bit tricky but in general these situation are the most common:

  1. You want to make the DataContext the the whole Window or Custom UserControl

.

<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.

like image 2
Mohammed Ehsan Avatar answered Oct 19 '22 00:10

Mohammed Ehsan