gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-04 06:28:48 25 4
gpt4 key购买 nike

我有一个 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com