相关疑难解决方法(0)

RelativeSource 适用于(嵌套)子属性,而 ElementName 不适用

下面代码的问题是:绑定到SomeClassProp.SubTextProp不起作用(源属性未设置为文本框内容),而绑定到TextProp它。

XAML:

<Window x:Class="TestWPF.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow"
        Name="wMain"
        SizeToContent="WidthAndHeight">
    <StackPanel>
        <TextBox Text="{Binding ElementName=wMain, Path=SomeClassProp.SubTextProp}" Width="120" Height="23" />
        <TextBox Text="{Binding ElementName=wMain, Path=TextProp}" Width="120" Height="23" />
    </StackPanel>
</Window>
Run Code Online (Sandbox Code Playgroud)

和代码:

public partial class MainWindow : Window
{
    public SomeClass SomeClassProp { get; set; }
    public string TextProp { get; set; }

    public MainWindow()
    {
        InitializeComponent();
        SomeClassProp = new SomeClass();
    }
}

public class SomeClass
{
    public string SubTextProp { get; set; }
}
Run Code Online (Sandbox Code Playgroud)

我在这里遗漏了一些明显的东西吗?

请注意,我需要此绑定才能从目标(文本框)到源(类属性)工作。

更新:当我将绑定更改ElementName=wMainRelativeSource={RelativeSource …

c# data-binding wpf xaml elementname

4
推荐指数
1
解决办法
1217
查看次数

标签 统计

c# ×1

data-binding ×1

elementname ×1

wpf ×1

xaml ×1