WPF绑定到子集合

时间:2010-08-10 00:18:25

标签: wpf data-binding collections

如果父级的数据源有一个子属性是一个集合(假设它叫做ChildCollection),有没有引用它的技巧?

所以,这个代码示例基本上就是我试图做的。但是当我使用这种方法时,我没有得到任何数据给我的孩子控件。

<UserControl>
    <UserControl.Resources>
        <sample:Data x:Key="MyData" />
    </UserControl.Resources>
    <Canvas DataContext="{StaticResource MyData}">
        <TextBlock Text="{Binding Title}" />
        <My:UserControl DataContext="{Binding ChildCollection}" />
    </Canvas>
</UserControl>

我的依赖属性如下所示:

public static readonly DependencyProperty DataProperty =
    DependencyProperty.Register("Data", typeof(IEnumerable), typeof(ButtonList), 
    new UIPropertyMetadata(new PropertyChangedCallback(DataChanged)));
public DoubleCollection Data
{
    get { return (DoubleCollection)GetValue(DataProperty); }
    set { SetValue(DataProperty, value); }
}
static void DataChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e)
{
    (sender as FrameworkElement).DataContext = e.NewValue;
}
public void SetData(IEnumerable data)
{
    (View as CollectionViewSource).Source = data;
}

提前感谢您的帮助。

2 个答案:

答案 0 :(得分:0)

如果我理解你的代码是正确的,你希望集合在UserControl的DataProperty中。

要实现这一点,你必须像这样进行Binding:

 <Canvas DataContext="{StaticResource MyData}">
    <TextBlock Text="{Binding Title}" />
    <My:UserControl Data="{Binding ChildCollection}" />
 </Canvas>

而不是:

<Canvas DataContext="{StaticResource MyData}">
    <TextBlock Text="{Binding Title}" />
    <My:UserControl DataContext="{Binding ChildCollection}" />
 </Canvas>

希望这有帮助。另外:我不知道画布是否将它的DataContext继承到childs。改为使用Panel(Grid / Stackpanel / WrapPanel)。

答案 1 :(得分:0)

下面是一个带有用户控件(ButtonList)的工作示例,其具有类型为IEnumerable<double>的DP,并为每个double值创建一个按钮。将它与您的代码进行比较,看看您做错了什么。

XAML:

<UserControl x:Class="UserControlDemo.ButtonList"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:UserControlDemo="clr-namespace:UserControlDemo"
    Name="_buttonList">
    <ItemsControl ItemsSource="{Binding Path=Data, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type UserControlDemo:ButtonList}}}">
        <ItemsControl.ItemTemplate>
            <DataTemplate>
                <Button Content="{Binding}" />
            </DataTemplate>
        </ItemsControl.ItemTemplate>
    </ItemsControl>
</UserControl>

代码背后:

using System;
using System.Collections.Generic;
using System.Windows;
using System.Windows.Controls;

namespace UserControlDemo
{
    public partial class ButtonList : UserControl
    {
        public ButtonList()
        {
            InitializeComponent();
        }

        public IEnumerable<double> Data
        {
            get { return (IEnumerable<double>)GetValue(DataProperty); }
            set { SetValue(DataProperty, value); }
        }

        public static readonly DependencyProperty DataProperty =
            DependencyProperty.Register("Data",
                                        typeof(IEnumerable<double>), 
                                        typeof(ButtonList), 
                                        new UIPropertyMetadata(new List<double>()));
    }
}

用法:

 <UserControlDemo:ButtonList Data="{Binding Path=Numbers}" />