开发者

Databind from XAML to code behind

开发者 https://www.devze.com 2023-03-01 15:29 出处:网络
I have this Text dependency property in code behind: public static DependencyProperty TextProperty = DependencyProperty.Register(\"Text\", typeof(string), typeof(MainWindow),

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. 开发者_如何学GoThe simplest code will be accepted.


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:

  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.


When you say it's in the code-behind, you mean it's in the code for the Window of your class?

You may want to bind to the RelativeSource where the ancestor type is Window. Alternatively, if your data context is not already set, in your Load event, set the window's DataContext property to the window itself (this), and just use {Binding Text}.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号