gpt4 book ai didi

wpf - 如何将 'bound' 值传递给依赖属性

转载 作者:行者123 更新时间:2023-12-04 20:36:57 25 4
gpt4 key购买 nike

我的应用程序有一个 MainWindow。其中有 1 个控件,一个 ListBox ,绑定(bind)到 MainWindowViewModel 的属性。此属性是 CriteriaVm 类型的 UserControl

CriteriaVm 有一个名为 MyString 的字符串属性

在标准 View 中,我有以下代码

<UserControl x:Class="CompoundInterests.View.CriteriaView"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:vm="clr-namespace:CompoundInterests.ViewModel"
mc:Ignorable="d"
d:DesignHeight="300" d:DesignWidth="600">

<UserControl.Resources>
<ResourceDictionary Source="../Dictionary.xaml" />
</UserControl.Resources>
<Grid>
<Border>
<Expander Header="{Binding MyString}" >
<vm:PropertiesVm ThresholdName="{Binding MyString}" />
</Expander>
</Border>
</Grid>
</UserControl>

如您所见,我在两个地方绑定(bind)了 MyString。 Expander 中的绑定(bind)工作正常,vm:PropertiesVm 中的绑定(bind)没有(它使用依赖属性)。输出窗口中显示以下错误

System.Windows.Data Error: 2 : Cannot find governing FrameworkElement or FrameworkContentElement for target element. BindingExpression:Path=MyString; DataItem=null; target element is 'PropertiesVm' (HashCode=5991653); target property is 'ThresholdName' (type 'String')



好的,错误消息告诉我它正在 ProperitesVm 中寻找 MyString ...我应该在 CriteriaVm 中寻找 MyString。这意味着我需要使用我认为的RelativeSource,它上升了1 级并且是UserControl 类型。所以我更新为:
<vm:PropertiesVm ThresholdName="{Binding Path=MyString, RelativeSource={RelativeSource AncestorLevel=1,AncestorType=UserControl,Mode=FindAncestor}}" />

我遇到了一个稍微不同的问题,但似乎存在相同的潜在故障

System.Windows.Data Error: 4 : Cannot find source for binding with reference 'RelativeSource FindAncestor, AncestorType='System.Windows.Controls.UserControl', AncestorLevel='1''. BindingExpression:Path=MyString; DataItem=null; target element is 'PropertiesVm' (HashCode=24649639); target property is 'ThresholdName' (type 'String')



目前,PropertiesVm 只有依赖属性,PropertiesView 只是一个空网格。这样我就可以先处理这个错误,然后再担心下一阶段的绑定(bind)。

我不明白为什么我会收到错误消息或我做错了什么。
如果需要,我可以很乐意提供更多代码。此阶段的项目非常早期,因此代码最少。

最佳答案

我希望 View 看起来像这样:

<Style TargetType="{x:Type ns:YourViewModel}">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type ns:YourViewModel}">
<Grid>
<TextBlock Text="{Binding ThresholdName,
RelativeSource={RelativeSource TemplatedParent}}"/>
</Grid>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>

View 模型代码与您显示的内容几乎没有变化:
public class YourViewModel: Control
{
public static readonly DependencyProperty ThreshNameProperty = DependencyProperty.Register("ThresholdName", typeof(string), typeof(PropertiesVm), new PropertyMetadata(string.Empty));

public string ThresholdName
{
get { return GetValue(ThreshNameProperty).ToString(); }
set { SetValue(ThreshNameProperty, value); }
}
}

您并不完全清楚“MyStringValue”是什么,所以我希望它是 MainWindow 的正常属性。 MainWindow 将创建您的控件的实例,将“ThresholdName”设置为此“MyStringValue”属性:
<Grid>
<ns:YourViewModel ThresholdName="{Binding MyStringValue}"/>
</Grid>

最后,您的 MainWindow 代码将是:
public string MyStringValue { get; set; }

public MainWindow()
{
InitializeComponent();
DataContext = this;
MyStringValue = "dog";
}

关于wpf - 如何将 'bound' 值传递给依赖属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33305274/

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