绑定对象列表的属性 [英] Bind properties of list of objects

查看:67
本文介绍了绑定对象列表的属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在制作一个由ListBox组成的WPF自定义控件。 CustomControl有一个DependencyProperty,它绑定到ListBox的ItemsSource。为了样本,让我们假设ListBox是具有名为Name的属性的Persons的集合,但是,它也可以在需要时用于其他类。



我尝试过:



ListBox如下所示:

I'm making a WPF Custom Control that consists of a ListBox. The CustomControl has a DependencyProperty that is bound to ItemsSource of the ListBox. For sample's sake, let's assume the ListBox is a collection of Persons with a property called Name, however, it can also be used for other classes as well when required.

What I have tried:

the ListBox looks like this:

<ListBox ItemsSource="{Binding Items, RelativeSource={RelativeSource TemplatedParent}">
     <ListBox.ItemTemplate>
         <DataTemplate>
             <TextBlock Text=""/>
         </DataTemplate>
     </ListBox.ItemTemplate>
</ListBox>





项目应绑定到类,TextBlock中的文本绑定到该类的某个属性。



属性如下所示:



Items is supposed to bind to a class and text in the TextBlock binds to a certain property of that class.

The Properties looks like this:

public static readonly DependencyProperty ItemsProperty = DependencyProperty.Register("Items", typeof(IEnumerable), typeof(SearchListBox), new FrameworkPropertyMetadata(null) { BindsTwoWayByDefault = true });
public static readonly DependencyProperty ClassPropertyProperty = DependencyProperty.Register("ClassProperty", typeof(string), typeof(SearchListBox), new FrameworkPropertyMetadata(null));

public IEnumerable SearchResults
{
   get { return (IEnumerable)GetValue(SearchResultsProperty); }
   set {SetValue(SearchResultsProperty, value); }
}





如何绑定Person的属性?



How can I bind to the property of Person?

推荐答案

我认为您可以使用 DisplayMemberPath 来通过 ClassProperty 动态选择命名属性显示:

I think you can use DisplayMemberPath to be able to dynamically select a named property through ClassProperty to display:
<ListBox ItemsSource="{Binding Items, RelativeSource={RelativeSource TemplatedParent}}"  DisplayMemberPath="{Binding ClassProperty, RelativeSource={RelativeSource TemplatedParent}}">
     </ListBox>



请注意 ItemTemplate



祝你好运。


Please note that ItemTemplate cannot be used when DisplayMemberPath is used.

Good luck.


如果你有列表框中的不同数据模板基于类型你应该这样做:



If you're going to have different data templates in the list box based on type you should do something like this:

<listbox itemssource="{Binding Items, RelativeSource={RelativeSource TemplatedParent}}">
    <listbox.resources>
        <datatemplate datatype="{x:Type viewModels:Person}">
            ...
        </datatemplate>
        <datatemplate datatype="{x:Type viewModels:OtherPossibleItemType}">
            ...
        </datatemplate>
    </listbox.resources>
</listbox>





无论您展示什么,它都会根据类型进行合适的显示,您无需添加不必要的元数这也是打算使用WPF的方式。



当然,如果您的对象来自更高级别的程序集,而这些程序集不适合此作用域的ListBox来确定模板,它们可以在可视树中更高的任何资源字典中定义。更重要的是,如果在其他控件中使用相同的模板,则应该在模块或应用程序级别提供,以避免冗余。



That way whatever you're displaying will have it's appropriate display based on type and you don't have to add unnecessary metadata. This is also the way WPF is intended to be used.

Of course if your objects are coming from higher level assemblies where it's not appropriate for this scoped ListBox to determine the template, they can be defined in any resource dictionary higher in the visual tree. And even more importantly, if the same template is used in other controls it should be available at the module or application level to avoid redundancy.


这篇关于绑定对象列表的属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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