WPF的简单绑定到一个对象的属性 [英] WPF Simple Binding to an objects property

查看:1366
本文介绍了WPF的简单绑定到一个对象的属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

林其在WPF / XAML绑定一些问题。有这个简单的文件:

Im having some problems with binding in wpf/xaml. Have this simple file:

<Window x:Class="test.Window1"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="Window1" Height="300" Width="300">
    <Grid>
        <TextBlock Height="21" Foreground="Black" Margin="74,98,84,0" Name="textBlock1" VerticalAlignment="Top" Text="{Binding MyText}" />
    </Grid>
</Window>

我想要的TextBlock的内容绑定到我的财产MYTEXT。我的code是这样的:

Where i want to bind the content of the textblock to my property "MyText". My code looks like this:

 public partial class Window1 : Window
    {
        public Window1()
        {
            InitializeComponent();
        }

        public string MyText
        {
            get { return "This is a test"; }
        }
    }

所有的一切很简单,但是当我开始正文块没有内容? - howcome

All in all very simple, but when i start the textblock has no content - howcome?

推荐答案

您需要的元素名称你的绑定:

you need an element name in your binding:

<Window ... x:Name="ThisWindow"...>

        <TextBlock ... Text="{Binding MyText, ElementName=ThisWindow}" />

这篇关于WPF的简单绑定到一个对象的属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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