【发布时间】:2010-10-01 09:01:11
【问题描述】:
简单的 WPF/XAML 问题。在 XAML 中,如何在给定的上下文中引用 Self/this 对象?在具有主窗口、一个控件和窗口的编码 C# 属性的非常基本的应用程序中,我想将控件的属性绑定到窗口的手动编码属性。
在代码中,这很容易——在 Window 的构造函数中,我添加了这个:
Binding bind = new Binding();
bind.Source = this;
bind.Path = new PropertyPath("ButtonWidth");
button1.SetBinding(WidthProperty, bind);
显然,我有一个名为 ButtonWidth 的属性和一个名为 button1 的控件。我无法弄清楚如何在 XAML 中执行此操作。像以下示例这样的各种尝试都没有奏效:
<Button x:Name="button1" Width="{Binding Source=Self Path=ButtonWidth}"/>
<Button x:Name="button1" Width="{Binding RelativeSource={RelativeSource Self} Path=ButtonWidth}"/>
等
谢谢
【问题讨论】: