在Silverlight中从子用户控件的视图模型绑定到视图模型? 2个来源-1个目标 [英] Binding from View-Model to View-Model of a child User Control in Silverlight? 2 sources - 1 target

查看:72
本文介绍了在Silverlight中从子用户控件的视图模型绑定到视图模型? 2个来源-1个目标的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

因此,我为一个视图提供了一个UserControl,在其中包含了另一个子 UserControl。

So i have a UserControl for one of my Views and have another 'child' UserControl inside that.

外部父 UserControl在其View-Model上具有一个Collection,并在其上具有一个Grid控件以显示 Items

The outer 'parent' UserControl has a Collection on its View-Model and a Grid control on it to display a list of Items.

我想在该UserControl中放置另一个UserControl,以显示一个表示一个 Item 的详细信息的表单。

I want to place another UserControl inside this UserControl to display a form representing the details of one Item.

父级UserControl的视图模型已经具有保存当前选定的 Item 的属性,我想将此绑定到子UserControl上的DependancyProperty。然后,我想将该DependancyProperty绑定到子UserControl的View-Model上的属性。

The parent UserControl's View-Model already has a property on it to hold the currently selected Item and i would like to bind this to a DependancyProperty on the child UserControl. I would then like to bind that DependancyProperty to a property on the child UserControl's View-Model.

然后我可以在XAML中使用绑定表达式设置DependancyProperty一次,并让子UserControl像应该的那样在其View-Model中完成所有工作。

I can then set the DependancyProperty once in XAML with a binding expression and have the child UserControl do all its work in its View-Model like it should.

我的代码如下所示。

父母UserControl:

Parent UserControl:

<UserControl x:Class="ItemsListView"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    DataContext="{Binding Source={StaticResource ServiceLocator}, Path=ItemsListViewModel}">
    <!-- Grid Control here... -->
    <ItemDetailsView Item="{Binding Source={StaticResource ServiceLocator}, Path=ItemsListViewModel.SelectedItem}" />
</UserControl>

Child UserControl:

Child UserControl:

<UserControl x:Class="ItemDetailsView"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    DataContext="{Binding Source={StaticResource ServiceLocator}, Path=ItemDetailsViewModel}"
    ItemDetailsView.Item="{Binding Source={StaticResource ServiceLocator}, Path=ItemDetailsViewModel.Item, Mode=TwoWay}"> 
    <!-- Form controls here... -->
</UserControl>

编辑:以下是我如何在子UC上创建依赖属性:

Heres how i created the Dependancy Proeprty on the child UC:

public partial class ItemDetailsView : UserControl
{
    private static readonly DependencyProperty itemProperty;

    static ItemDetailsView()
    {
        ItemDetailsView.itemProperty = DependencyProperty
            .Register("Item", typeof(Item), typeof(ItemDetailsView), null);
    }

    public Item Item
    {
        get { return (Item)GetValue(ItemDetailsView.itemProperty); }
        set { SetValue(ItemDetailsView.itemProperty, value); }
    }

    public static Item GetItem(DependencyObject target)
    {
        return (Item)target.GetValue(itemProperty);
    }

    public static void SetItem(DependencyObject target, Item value)
    {
        target.SetValue(itemProperty, value);
    }
}

选定的项目受DependancyProperty罚款的约束。但是,从DependancyProperty到子级View-Model却没有。

The selected Item is bound to the DependancyProperty fine. However from the DependancyProperty to the child View-Model does not.

似乎存在这样一种情况,需要同时进行两个并发绑定,但是两个源具有相同的目标。

It appears to be a situation where there are two concurrent bindings which need to work but with the same target for two sources.

为什么第二个绑定(在子UserControl中)不起作用?有没有办法实现我所追求的行为?

Why won't the second (in the child UserControl) binding work?? Is there a way to acheive the behaviour I'm after??

干杯。

推荐答案

好吧,看来您正在尝试在父UserControl上使用常规 DependencyProperty,在子UserControl上使用附加 DependencyProperty。您需要选择一种方式。 :)

Well, it looks like you are trying to use a "normal" DependencyProperty on the parent UserControl, and an "attached" DependencyProperty on the child UserControl. You need to pick one way. :)

为澄清起见,请编辑:
有两种方式注册依赖项属性 Normal,如下所示:

EDIT for clarification: There are two ways of registering a dependency property, "Normal", like so:

public static readonly DependencyProperty BobProperty = 
    DependencyProperty.Register("Bob",....)

并附加:

public static readonly DependencyProperty BobAttachedProperty = 
    DependencyProperty.RegisterAttached("BobAttached",...)

说说控件您在上面注册这些属性的方法称为 MyPanel。要使用每个属性,请执行以下操作:

Let's say the control you are registering these properties on is called "MyPanel". To use each property:

<MyPanel Bob="somevalue" MyPanel.BobAttached="somevalue"/>

请注意需要指定定义附加属性的位置。当您具有适用于多种控件类型的某些行为或功能时,附加属性非常有用。

Note the need to specify "where the attached property is defined". Attached properties are great when you have some bit of behavior or functionality that applies to multiple types of controls.

也就是说,也许有更好的方法可以执行此操作-如果父UserControl包含一个ItemsControl,该控件的ItemTemplate可以是包含ItemDetailsView的DataTemplate,在这种情况下,您可以使用标准数据绑定来执行所需的操作:

That said, perhaps there is a better way to do this - If the parent UserControl contained an ItemsControl, the ItemTemplate for that control could be a DataTemplate that contained the ItemDetailsView, in which case you could use standard data binding to do what you needed to:

<UserControl blahblahblah>
    <ItemsControl ItemsSource="{Binding WhereYourItemsAre}">
        <ItemsControl.ItemTemplate>
            <DataTemplate>
               <ns:WhatYourChildViewIsCalled DataContext="{Binding}"/>
            </DataTemplate>
        </ItemsControl.ItemTemplate>
    </ItemsControl>
</UserControl>

这篇关于在Silverlight中从子用户控件的视图模型绑定到视图模型? 2个来源-1个目标的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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