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}" />