带有用户控件的 WPF 数据绑定(bind)

标签 wpf data-binding user-controls

我有一个 wpf 用户控件,它公开了一个自定义依赖属性。在用户控件内部,一个文本 block 绑定(bind)到 dp 的值。此数据绑定(bind)适用于所有场景,但数据源是对象时除外。

重现这一点所需的最少代码是:

这是用户控件的主要部分

<StackPanel Orientation="Horizontal">
    <TextBlock Text="**SimpleUC** UCValue: "/>
    <TextBlock Text="{Binding UCValue}"/>
</StackPanel>

以及后面的用户控制代码:
    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"));

这是测试窗口。我将我的项目 xml 命名空间导入为“自定义”
<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>

后面的测试窗口代码是:
public TestWindow()
{
    InitializeComponent();
    this.DataContext = this;
}

//property to bind to
public string SomeData { get { return "Hello S.O."; } } 

当我在 TestWindow 上打开诊断跟踪时,它会吐出错误“BindingExpression 路径错误:'SomeData' property not found on 'object' ''SimpleUC' (Name='')' ... "绑定(bind)表达式与我在相邻标签中使用的表达式相同,并且效果很好。这种行为对我来说真的很奇怪。任何人都可以解释一下吗?

最佳答案

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

public SimpleUC()
{
    InitializeComponent();
    this.DataContext = this; // wrong way!
}

所以当你在这里使用绑定(bind)时
<custom:SimpleUC UCValue="{Binding SomeData}"/>

它在控件的数据上下文中搜索属性 SomeData 设置为此对象,因为 SimpleUC 构造函数中的代码覆盖了 DataContext 的值,并且它不再像您预期的那样设置为 TestWindow 对象。这就是您的解决方案有效的原因 - 它不会影响从 window.context 继承的 DataContext。您也可以保留this.DataContext = this;但是设置元素在哪里明确搜索属性(跳过无关)
<Window ... Name="wnd1">
    <custom:SimpleUC UCValue="{Binding SomeData, ElementName=wnd1}"/>
...

但是我的意见是,您的答案变体对我来说看起来更方便,将数据上下文设置为此不是很好的做法。

希望能帮助到你。

关于带有用户控件的 WPF 数据绑定(bind),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2255001/

相关文章:

wpf - 如何将顶级 MenuItem 上的 Visibility 属性绑定(bind)到 ViewModel

.net - 用户控件数据绑定(bind)的最佳实践 - 如何实现?

c# - 带有 winforms 和 C# 的 this.Controls.Add() 上的 System.NullReferenceException

c# - 在 WPF 中使用 Unity Prism 在 TextBox 中键入时实时更新 TextBlock

c# - 程序不包含适合入口点的静态 'main' 方法

wpf - 想要更改 wpf datagrid 中的单元格背景颜色

javascript - AngularJS/Parse.com : $scope. currentUser 变量未更新

javascript - RactiveJS 将多个对象绑定(bind)到模板

c# - 在 View 状态中存储 List<int>

c# - 使 AvalonEdit MVVM 兼容