自定义DependencyProperty未绑定到ElementName



我决定在视图模型中对绑定依赖属性进行一些实验。为此,我设置了一个简单的测试项目,由MainWindow.xaml:组成

<Window x:Class="MVVM_Test.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:MVVM_Test"
xmlns:diag="clr-namespace:System.Diagnostics;assembly=WindowsBase"
xmlns:sys="clr-namespace:System;assembly=mscorlib"
mc:Ignorable="d"
Title="MainWindow" Height="350" Width="525">
<Grid>
<Grid.RowDefinitions>
<RowDefinition />
<RowDefinition />
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition />
<ColumnDefinition />
</Grid.ColumnDefinitions>
<ListBox Margin="6" Grid.RowSpan="2" Name="PART_LISTBOX">
<sys:String>String 1</sys:String>
<sys:String>String 2</sys:String>
<sys:String>String 3</sys:String>
</ListBox>
<StackPanel Name="PART_spViewModel2" Grid.Column="1" Grid.Row="1">
<StackPanel.DataContext>
<local:ViewModel2 BoundString="{Binding ElementName=PART_LISTBOX,
Path=SelectedItem,
diag:PresentationTraceSources.TraceLevel=High}" />
</StackPanel.DataContext>
<TextBox Margin="6" Text="{Binding BoundString}" />
</StackPanel>
</Grid>
</Window>

并由一个简单的视图模型对象备份:

public class ViewModel2 : DependencyObject
{
public string BoundString
{
get { return (string)GetValue(BoundStringProperty); }
set { SetValue(BoundStringProperty, value); }
}
// Using a DependencyProperty as the backing store for BoundString.
public static readonly DependencyProperty BoundStringProperty =
DependencyProperty.Register("BoundString", typeof(string), typeof(ViewModel2), 
new FrameworkPropertyMetadata(default(string),
FrameworkPropertyMetadataOptions.BindsTwoWayByDefault ));
}

现在,我希望在ListBox中选择某个内容后,Textbox的内容会发生更改。然而,当我运行该程序时,DataBinding:似乎找不到ListBox

系统。Windows。数据警告:56:已为绑定(hash=440205895)创建BindingExpression(hash=61931053)

系统。Windows。数据警告:58:路径:"SelectedItem">

系统。Windows。数据警告:60:BindingExpression(hash=61931053):默认模式解析为TwoWay

系统。Windows。数据警告:61:BindingExpression(hash=61931053):默认更新触发器已解析为PropertyChanged

系统。Windows。数据警告:62:BindingExpression(hash=61931053):附加到MVVM_Test。ViewModel2.BoundString(hash=64815892)

系统。Windows。数据警告:64:BindingExpression(hash=61931053):使用Framework导师

系统。Windows。数据警告:67:BindingExpression(hash=61931053):正在解析源

系统。Windows。数据警告:69:BindingExpression(hash=61931053):找不到框架导师

系统。Windows。数据警告:65:BindingExpression(hash=61931053):解析源延迟

延迟查找会继续一段时间,直到达到以下点:

系统。Windows。数据警告:67:BindingExpression(hash=61931053):正在解析源(最后一次机会)

系统。Windows。数据警告:69:BindingExpression(hash=61931053):找不到框架导师

系统。Windows。数据错误:2:找不到目标元素的管理FrameworkElement或FrameworkContentElement。BindingExpression:Path=SelectedItem;DataItem=null;目标元素是"ViewModel2"(哈希代码=64815892);目标属性为"BoundString"(类型为"String")

如果我以另一种方式进行绑定,例如从ListBox.SelectedItem属性到ViewModel,它就会起作用。

<ListBox Margin="6" Grid.RowSpan="2" Name="PART_LISTBOX" 
SelectedItem="{Binding ElementName=PART_spViewModel2,
Path=DataContext.BoundString}">

所以我的猜测是我没有正确设置DependencyProperty,但我到底在哪里犯了错误?

您将视图中的目标属性绑定到DataContext的source特性。视图模型是DataContext,即它定义了源属性。它不会添加到可视化树中,因此您不能将BoundString依赖项属性用作ListBox的SelectedItem属性的目标属性。

通常,视图模型中的源属性被定义为CLR属性,而目标属性,即您绑定到的属性,必须被定义为依赖属性。您可以参考以下问题了解更多信息:

有人能举例说明ViewModel 中的依赖属性吗

ViewModel 中的INotifyPropertyChanged与DependencyProperty

最新更新