绑定不适用于 DependencyProperty
我有一个窗口,其中有一个用户控件。此用户控件的 RequestObject 属性绑定到窗口 ViewModel 的 SearchArgumentObject 属性。
这是我的窗口类中的列表
<Grid DataContext="{Binding SearchArgumentObject, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}">
<guiLib:RegCardSearchForm x:Name="SearchParametrsUC" Grid.Row="1" RequestObject="{Binding .,Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" />
</Grid>
在 Usercontrol 类中,我创建了依赖属性:
这是我的 userControl 类中的列表
public static DependencyProperty RequestObjectProperty = DependencyProperty.Register("RequestObject", typeof(RegistrationCardSearch), typeof(RegCardSearchForm));
public RegistrationCardSearch RequestObject
{
get
{
return (RegistrationCardSearch)GetValue(RequestObjectProperty);
}
set
{
SetValue(RequestObjectProperty, value);
}
}
在 usecontrol 级别上,一切正常,RequestOject 属性已更改。
但在我的窗口类中,我看不到在用户控件中对 SearchArgumentObject 属性进行的修改。
如何获得修改后的财产价值?我认为这个问题的答案非常微不足道,但我找不到解决方案。
I have a window which has a usercontrol in it . This usercontrol's RequestObject property bound to SearchArgumentObject property of ViewModel of the window.
This is listing from my window class
<Grid DataContext="{Binding SearchArgumentObject, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}">
<guiLib:RegCardSearchForm x:Name="SearchParametrsUC" Grid.Row="1" RequestObject="{Binding .,Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" />
</Grid>
In Usercontrol class I created dependency property:
This is listing from my userControl class
public static DependencyProperty RequestObjectProperty = DependencyProperty.Register("RequestObject", typeof(RegistrationCardSearch), typeof(RegCardSearchForm));
public RegistrationCardSearch RequestObject
{
get
{
return (RegistrationCardSearch)GetValue(RequestObjectProperty);
}
set
{
SetValue(RequestObjectProperty, value);
}
}
On the level of the usecontrol everything works fine and RequestOject property changed.
But in my window class I can't see modification of SearchArgumentObject property which was made in usercontrol.
How can I get modefied property value? I think answer to this question is very trivial but I can't find solution.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
在网格上设置 DataContext 不会执行任何操作,只会破坏属性的双向链接。跳过额外的步骤,并将 VM 属性绑定到您想要从中获取更改的控件属性:
Setting the DataContext on the Grid isn't doing anything but breaking the two-way linking of your properties. Skip the extra step and bind the VM property to the control property that you want to pick up changes from instead:
Window 类的代码将
Grid
的DataContext
设置为从绑定到另一个对象的DataContext
上的属性获得的属性树。您是否在其他地方设置了 Window 的 DataContext?假设提供
SearchArgumentObject
的对象名为SearchWindowViewModel
。在 Window 的代码隐藏中,您将具有以下代码(例如,在构造函数中):现在,
SearchWindowViewModel
公开的所有属性均可用于 Window。要将SearchWindowViewModel.SearchArgumentObject
绑定到 UserControl 的RequestObject
属性,您将具有以下 XAML:如果您不想设置窗口的
DataContext
,您可以使用与我上面使用的相同类型的代码来设置网格的DataContext
,并且 XAML 中的绑定将保持不变。希望有帮助。
The code for your Window class is setting the
DataContext
of theGrid
to a property obtained from a binding to a property on another object'sDataContext
further up the tree. Do you have the Window'sDataContext
set elsewhere?Let's say that the object which is supplying the
SearchArgumentObject
is namedSearchWindowViewModel
. In the code-behind of the Window, you would have the following code (in the constructor, for example):Now, all the properties that
SearchWindowViewModel
exposes are available to the Window. To bind theSearchWindowViewModel.SearchArgumentObject
to the UserControl'sRequestObject
property, you would have the following XAML:If you don't want to set the Window's
DataContext
, you can set the Grid'sDataContext
using the same type of code as I used above, and the binding in the XAML would remain the same.Hope that helps.