简单的WPF/XAML问题。在XAML中,如何在给定上下文中引用Self/this对象?在一个非常基本的应用程序中,有一个主窗口、一个控件和窗口的一个手动编写的C#属性,我想将控件的属性绑定到窗口的手动编写的属性。
在代码中,这很容易 - 在窗口的构造函数中,我添加了这个:
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}"/>
等等
谢谢