绑定到在XAML用户控件的集合属性物品 [英] Binding to user control's collection property items in xaml

查看:154
本文介绍了绑定到在XAML用户控件的集合属性物品的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经创建了集合属性的用户控件:

I have created a user control with collection property:

    public static readonly DependencyProperty
    MyListProperty = DependencyProperty.Register(
        "MyList",
        typeof(ObservableCollection<Test>),
        typeof(UserControl1),
        new FrameworkPropertyMetadata(new ObservableCollection<Test>())
        );

    public ObservableCollection<Test> MyList
    {
        get { return (ObservableCollection<Test>)base.GetValue(MyListProperty); }
        set { base.SetValue(MyListProperty, value); }
    }

    public static readonly DependencyProperty
    BProperty = DependencyProperty.Register(
        "B",
        typeof(string),
        typeof(UserControl1),
        new FrameworkPropertyMetadata(null)
    );

    public string B
    {
        get { return (string)base.GetValue(BProperty); }
        set { base.SetValue(BProperty, value); }
    }

测试类是:

public class Test : DependencyObject
{
    public static readonly DependencyProperty
    AProperty = DependencyProperty.Register(
        "A",
        typeof(string),
        typeof(Test),
        new FrameworkPropertyMetadata(null)
    );

    public string A 
    {
        get { return (string)base.GetValue(AProperty); }
        set { base.SetValue(AProperty, value); }
    }
}

然后,我试图用我的绑定控件:

Then, i'm trying to use my control for binding:

    <TextBox x:Name="tb1" Text="def"/>
    <my:UserControl1 x:Name="uc1" B="{Binding ElementName=tb1, Path=Text}">
        <my:UserControl1.MyList>
            <my:Test A="{Binding ElementName=tb1, Path=Text}"></my:Test>
            <my:Test A="100"></my:Test>
        </my:UserControl1.MyList>
    </my:UserControl1>

第一个结合(与B的用户控件的属性)正常工作。问题是与第二结合(与试验的一种属性,它是MYLIST元素)。调试时我在MYLIST两个项目,但在第一个的属性是否为null。请告诉我什么,我在这里丢失?

The first binding (with B property of User Control) works correctly. The problem is with second binding (with A property of Test which is MyList element). When debugging i have two items in MyList but the A property of the first one is null. Please tell me what I am missing here?

推荐答案

这是问题所在,即绑定到的ElementName = TB1不能得到解决,即使它永远不会被评估。一个绑定到一个elementName为解决的DependencyObjects这是在WPF应用程序的视觉或逻辑树。将项目添加到您的ObservableCollection(MYLIST)仅意味着添加的项目集合,而不是到Visual树。

The problem here is, that the Binding to ElementName=tb1 can not be resolved, even it will never be evaluated. A Binding to an ElementName is resolved for DependencyObjects which are in the visual or logical Tree of a WPF Application. Adding items to your ObservableCollection (MyList) only means adding the items to the Collection, but not into the Visual Tree.

修改
这是在评论中讨论的方法:

Edit: Here is the approach discussed in the comments:

在你的Window /页:

In your Window/Page:

<Window.Resources>
    <!-- Declare the ViewModel as Resource -->
    <my:ViewModel x:Key="viewModel">
        <my:ViewModel.MyList>
            <my:Test A="Hello sweet" />
            <my:Test A="ViewModel" />
        </my:ViewModel.MyList>
    </my:ViewModel>
</Window.Resources>

<!-- Assign Ressource as DataContext -->
<StackPanel DataContext="{StaticResource viewModel}">

    <TextBox x:Name="tb1" Text="def"/>

    <!-- Reference your list within the ViewModel -->
    <ListBox ItemsSource="{Binding Path=MyList}">
        <ListBox.ItemTemplate>
            <DataTemplate>
                <!-- Bind your property  -->
                <TextBlock Text="{Binding Path=A}" />
            </DataTemplate>
        </ListBox.ItemTemplate>
    </ListBox>
</StackPanel>

和视图模型的实现:

public class ViewModel
{
    public ViewModel()
    {
        this.MyList = new ObservableCollection<Test>();
    }

    public ObservableCollection<Test> MyList { get; set; }
}

当然,Test类不再需要实现的DependencyObject。简单的get / set属性都还可以。

Of course, class Test no longer needs to implement a DependencyObject. Simple get/set Properties are okay.

这篇关于绑定到在XAML用户控件的集合属性物品的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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