WPF与用户控件的数据绑定

dan*_*dan 8 data-binding wpf user-controls

我有一个wpf用户控件,它公开了一个自定义依赖项属性.在用户控件内,文本块绑定到dp的值.此数据绑定适用于所有方案,除非数据源是对象.

重现这个的最小代码是:

这是用户控件的主要部分

<StackPanel Orientation="Horizontal">
    <TextBlock Text="**SimpleUC** UCValue: "/>
    <TextBlock Text="{Binding UCValue}"/>
</StackPanel>
Run Code Online (Sandbox Code Playgroud)

以及后面的用户控制代码:

    public SimpleUC()
    {
        InitializeComponent();
        this.DataContext = this;
    }

    public string UCValue
    {
        get { return (string)GetValue(UCValueProperty); }
        set { SetValue(UCValueProperty, value); }
    }

    public static readonly DependencyProperty UCValueProperty =
        DependencyProperty.Register("UCValue", typeof(string), typeof(SimpleUC), new UIPropertyMetadata("value not set"));
Run Code Online (Sandbox Code Playgroud)

这是测试窗口.我将项目xml命名空间导入为"custom"

<Window.Resources>
    <Style TargetType="{x:Type StackPanel}">
        <Setter Property="Margin" Value="20"/>
    </Style>
</Window.Resources>
<StackPanel>
    <StackPanel>
        <TextBlock Text="This fails to bind:"/>
        <custom:SimpleUC UCValue="{Binding SomeData}"/> 
    </StackPanel>
    <StackPanel>
        <TextBlock>The same binding on a regular control like Label</TextBlock>
        <Label Content="{Binding SomeData}"/>
    </StackPanel>
    <Slider x:Name="sld" />
    <StackPanel>
        <TextBlock>However, binding the UC to another element value, like a slider works</TextBlock>
        <custom:SimpleUC UCValue="{Binding ElementName=sld,Path=Value}"/>
    </StackPanel>
</StackPanel>
Run Code Online (Sandbox Code Playgroud)

并且后面的测试窗口代码是:

public TestWindow()
{
    InitializeComponent();
    this.DataContext = this;
}

//property to bind to
public string SomeData { get { return "Hello S.O."; } } 
Run Code Online (Sandbox Code Playgroud)

当我打开TestWindow上的诊断跟踪时,它会发出错误"BindingExpression路径错误: 'SomeData' property not found on 'object' ''SimpleUC' (Name='')' ... "
绑定表达式与我在相邻标签中使用的表达式相同,它运行正常.这种行为对我来说似乎很奇怪.可以有人放光吗?

lev*_*ovd 8

您可以在此处将SimpleUC的DataContext设置为自身

public SimpleUC()
{
    InitializeComponent();
    this.DataContext = this; // wrong way!
}
Run Code Online (Sandbox Code Playgroud)

所以当你在这里使用绑定

<custom:SimpleUC UCValue="{Binding SomeData}"/>
Run Code Online (Sandbox Code Playgroud)

它在控件的数据上下文中搜索属性SomeData,该上下文设置为此对象,因为SimpleUC构造函数中的代码会覆盖DataContext的值,并且不再按预期将其设置为TestWindow对象.这就是您的解决方案有效的原因 - 它不会影响从窗口继承的DataContext.你也可以保留,this.DataContext = this;但设置元素在哪里显式搜索属性(跳过不相关)

<Window ... Name="wnd1">
    <custom:SimpleUC UCValue="{Binding SomeData, ElementName=wnd1}"/>
...
Run Code Online (Sandbox Code Playgroud)

但我的观点是,你的答案中的变体看起来对我来说更方便,设置数据上下文对此并不是很好的做法.

希望能帮助到你.