绑定不适用于 DependencyProperty

发布于 2024-09-27 06:35:11 字数 1153 浏览 2 评论 0原文

我有一个窗口,其中有一个用户控件。此用户控件的 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

离线来电— 2024-10-04 06:35:11

在网格上设置 DataContext 不会执行任何操作,只会破坏属性的双向链接。跳过额外的步骤,并将 VM 属性绑定到您想要从中获取更改的控件属性:

<Grid>
    <guiLib:RegCardSearchForm x:Name="SearchParametrsUC" Grid.Row="1"
           RequestObject="{Binding SearchArgumentObject, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" />
</Grid>

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:

<Grid>
    <guiLib:RegCardSearchForm x:Name="SearchParametrsUC" Grid.Row="1"
           RequestObject="{Binding SearchArgumentObject, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" />
</Grid>
独行侠 2024-10-04 06:35:11

Window 类的代码将 GridDataContext 设置为从绑定到另一个对象的 DataContext 上的属性获得的属性树。您是否在其他地方设置了 Window 的 DataContext?

假设提供 SearchArgumentObject 的对象名为 SearchWindowViewModel。在 Window 的代码隐藏中,您将具有以下代码(例如,在构造函数中):

DataContext = new SearchWindowViewModel();

现在,SearchWindowViewModel 公开的所有属性均可用于 Window。要将 SearchWindowViewModel.SearchArgumentObject 绑定到 UserControl 的 RequestObject 属性,您将具有以下 XAML:

<Grid>
  <guiLib:RegCardSearchForm x:Name=SearchParametersUC Grid.Row=1 
          RequestObject={Binding SearchArgumentObject />
</Grid>

如果您不想设置窗口的 DataContext,您可以使用与我上面使用的相同类型的代码来设置网格的 DataContext,并且 XAML 中的绑定将保持不变。

希望有帮助。

The code for your Window class is setting the DataContext of the Grid to a property obtained from a binding to a property on another object's DataContext further up the tree. Do you have the Window's DataContext set elsewhere?

Let's say that the object which is supplying the SearchArgumentObject is named SearchWindowViewModel. In the code-behind of the Window, you would have the following code (in the constructor, for example):

DataContext = new SearchWindowViewModel();

Now, all the properties that SearchWindowViewModel exposes are available to the Window. To bind the SearchWindowViewModel.SearchArgumentObject to the UserControl's RequestObject property, you would have the following XAML:

<Grid>
  <guiLib:RegCardSearchForm x:Name=SearchParametersUC Grid.Row=1 
          RequestObject={Binding SearchArgumentObject />
</Grid>

If you don't want to set the Window's DataContext, you can set the Grid's DataContext using the same type of code as I used above, and the binding in the XAML would remain the same.

Hope that helps.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文