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