我想将一个文本框绑在窗口的属性上, 该属性在文件后面的代码中定义 。
当使用“ FindAsentor ” 设置相对源来查找窗口时, 当引用窗口时, 绑定成功 。
为什么它不能用它的名字来称呼窗口, 就像我可以把窗口的“标题”属性绑起来一样?
<强>XAML: 强>
<Window x:Class="WpfApplication123.MainWindow"
x:Name="MyWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="WPF Binding Example" Height="180" Width="237">
<Grid>
<TextBox Height="23" HorizontalAlignment="Left" Margin="24,24,0,0" Name="textBox1" VerticalAlignment="Top" Width="136"
Text="{Binding ElementName=MyWindow, Path=Title}"/>
<TextBox Height="23" HorizontalAlignment="Left" Margin="24,53,0,0" Name="textBox2" VerticalAlignment="Top" Width="136"
Text="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type Window}}, Path=XYZ}"/>
<TextBox Height="23" HorizontalAlignment="Left" Margin="24,82,0,0" Name="textBox3" VerticalAlignment="Top" Width="136"
Text="{Binding ElementName=MyWindow, Path=XYZ}" />
</Grid>
</Window>
后端 < 坚固 > 代码: 坚固>
namespace WpfApplication123
{
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
XYZ = "XYZ!";
}
public string XYZ { get; set; }
}
}