2010-08-10 221 views
0

如果父级的数据源有一个属性是集合的子属性(假设它被称为ChildCollection)是否有引用它的技巧?WPF绑定到子集合

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

<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; 
} 

预先感谢您的帮助。

回答

0

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

要做到这一点,你必须做这样的绑定:

<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的孩子。改为使用面板(Grid/Stackpanel/WrapPanel)。

+0

PS:也尝试DynamicResource替代StaticResource的为您的DataContext面板上的绑定。 – JanW 2010-08-10 14:21:43

+0

画布来自Panel。所有FrameworkElements都提供了DataContext继承。 – 2010-08-11 05:42:04

+0

那是不对的。框架组件不会继承它的DataContext。因为我没有使用Canvas进行测试,所以我提到了它。 – JanW 2010-08-11 06:37:15

0

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

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}" />