将通用列表传递给WPF用户控件

时间:2010-01-14 08:11:46

标签: wpf list user-controls

我想创建一个带有不同对象列表的usercontrol。这些对象将在设计时分配给控件。现在我希望能够使用linq来对象在usercontrol中对此列表进行排序。任何人都可以给我任何想法,如何去做?

1 个答案:

答案 0 :(得分:3)

DependencyProperty类型ObservableCollection<T>添加到您的用户控件类(例如,将其称为MyItemsSource)。在您的包含XAML中,将该属性绑定到Linq集合,并在您的用户控件中,将ListBox(或其他ItemsControl)绑定到属性,如下所示:

{Binding 
    RelativeSource={RelativeSource 
                        Mode=FindAncester, 
                        AncestorType=UserControl}, 
    Path=MyItemsSource}

或者,您可以将顶级元素(Name元素)上的用户控件内的UserControl属性设置为MyUserControl,并绑定ElementName而不是RelativeSource本身:

{Binding ElementName=MyUserControl, Path=MyItemsSource}