如何绑定到 XAML 代码隐藏中定义的属性

Vah*_*hid 1 c# wpf xaml

如何将Text的属性绑定TextBoxFoundationHeight代码隐藏中定义的 clr-property。

xaml

<TextBox Text="{Binding FoundationHeight}"/>
Run Code Online (Sandbox Code Playgroud)

C#

public double FoundationHeight { get; set; }
public AssignColumnPropertiesWindow()
{
    InitializeComponent();
    FoundationHeight = 60;
}
Run Code Online (Sandbox Code Playgroud)

Dam*_*cus 5

使用RelativeSource

如果为您的 UserControl 定义了代码隐藏:

<TextBox Text="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type UserControl}}, Path=FoundationHeight}" />
Run Code Online (Sandbox Code Playgroud)