数据绑定XAML的背后code [英] Databind from XAML to code behind

查看:107
本文介绍了数据绑定XAML的背后code的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这样的文本在身后code依赖属性:

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 ?

我已经做了一些时间,但现在我不记得如何 - 这是很简单的。最简单的code将被接受。

I have done that some time before but now I cannot remember how - and it is very simple. The simplest code will be accepted.

推荐答案

你的窗口/控制同一类的设置的DataContext,然后在指定的结合,像这样的路径:

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

然后在您的XAML:

Then in your xaml:

<Label Content="{Binding Path=Text}">

这篇关于数据绑定XAML的背后code的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆