WPF级联绑定 [英] WPF Cascading Binding

查看:173
本文介绍了WPF级联绑定的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

主题:如何prevent上一个用户控件绑定覆盖由使用该用户控件上(主)窗口结合给定

Topic: How to prevent that the binding on a UserControl overrides the binding given by the upper (Main)Window using this UserControl.

我已经看过帖子,在这个问题上的一些书籍,但我仍然困惑与绑定机制。
即使在一些专业书籍中的条款的的DependencyProperty AttachedProperty 的是很少被提及。
微软的文档是好的,但有些光秃秃的。

I already read posts, and some books on that subject but I am still puzzled with binding mechanisms. Even in some specialized books the terms of DependencyProperty or AttachedProperty is barely mentioned. Microsoft documentation is good but somewhat bare.

谢谢你再次把我在正确的方向

Thanks for putting me again on the right direction

这例子是深入浅出C#

我有一个用户控件,一个简单的圈子

I have a usercontrol, a simple circle

...  
xmlns:vm_nmspc="clr-namespace:BindingMCVE.ViewModel" >

<UserControl.Resources>
    <vm_nmspc:my_usercontrol_vm x:Key="MyUserControlVM"/>
</UserControl.Resources>

<Grid>
    <StackPanel  DataContext="{DynamicResource ResourceKey=MyUserControlVM}" Height="100"  Width="100">
        <Ellipse Fill="{Binding my_color}" Height="50" Width="50"/>
    </StackPanel>
</Grid>

和我喜欢的是提供的无参构造器 my_usercontrol_vm()的,这样我可以查看我的圈子里的用红色填充,而我工作在我的用户控件的看法。

And what I like is to provide a parameterless contructor my_usercontrol_vm() so that I can view my circle filled with red while I am working on the view of my UserControl.

有关,我使用的的my_usercontrol_vm的实例中的 UserControlResources
在这里,我用的 DynamicResource 的关键字(我第一次测试的静态资源)

For that I use an instance of the class my_usercontrol_vm inside the UserControlResources. Here I use the DynamicResource keyword ( I had first tested StaticResource )

class my_usercontrol_vm
{
    public string my_color {get; set;}

    public my_usercontrol_vm() : this("Red") { }

    public my_usercontrol_vm(string color)
    {
        my_color = color;
    }
}

现在我想用一个(主)窗口,里面还有几个用户控件的使用绑定即可看到的实时的我的看法的演变。

Now I want to use several UserControl inside a (Main)Window, still using binding to see in "real time" the evolution of my view.

xmlns:vm_nmspc="clr-namespace:BindingMCVE.ViewModel" >

<Window.Resources>
    <vm_nmspc:main_window_vm x:Key="MainWindowVM"/>   
</Window.Resources>

<Grid>
    <StackPanel DataContext="{DynamicResource ResourceKey=MainWindowVM}">

        <view_nmspc:UserControl1 DataContext="{Binding usr_ctrl_1}"/>
        <view_nmspc:UserControl1 DataContext="{Binding usr_ctrl_2}"/>

    </StackPanel>
</Grid>
</Window>

以及视图模型的类献给主窗口

class main_window_vm
{
    public my_usercontrol_vm usr_ctrl_1 { get; set; }
    public my_usercontrol_vm usr_ctrl_2 { get; set; }

    public main_window_vm()
    {
        usr_ctrl_1 = new my_usercontrol_vm("Green");
        usr_ctrl_2 = new my_usercontrol_vm("Blue");
    }
}

请注意,我的希望可查看的绿色蓝色实心圆。

Note that I am expecting to see GREEN and BLUE filled circles.

但是我得到的是的红色的圈子

However what I get is red circles

我会得到绿色和蓝色的圆圈我没有把资源的用户控件

I would have got green and blue circle I had not put a resource inside the UserControl

在使用调试器,我可以看到,我进入第一类的构造函数中的 main_window_vm 的后(2次)类之一的 my_user_control_vm 的,从而使两个圆终于红了。

While using the debugger I can see that I am entering first the constructor of the class main_window_vm and after (2 times) the one of the class my_user_control_vm, so that both circles are finally red.

我知道,在present时候我也许知道WPF的10%,但我认为这样做的这种方式会一直很正确。截至去年我请求的主窗口内的的UserControl1 的控制的被绑定应用于类的属性usr_ctrl_X的 main_window_vm

I know that at the present time I am maybe 10% aware of WPF but I thought that this way of doing would have been quite right. As last I am requesting that inside the MainWindow the UserControl1 control be bind to the property usr_ctrl_X of the class main_window_vm

任何意见欢迎。

最好的问候。

NGI

推荐答案

您正在设置的DataContext 你的用户控件,但这不是由您的椭圆,因为这继承了的DataContext 的 StackPanel中。这不会改变,将永远被绑定到你的 MyUserControlVM 资源。

You are setting the DataContext of your UserControl, but this isn't used by your Ellipse as this inherits the DataContext of the StackPanel. This doesn't change and will always be bound to your MyUserControlVM resource.

您应该删除的DataContext ={DynamicResource的ResourceKey = MyUserControlVM}的StackPanel 。如果您将其添加到用户控件那么你应该结合覆盖它。你也可以使用混合的设计命名空间设置一个设计的DataContext ,只会在设计时使用。

You should remove DataContext="{DynamicResource ResourceKey=MyUserControlVM}" from the StackPanel. If you add this to the UserControl then your binding should overwrite it. You could also use the blend design namespaces to set a design DataContext that would only be used at design time.

这篇关于WPF级联绑定的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆