简体   繁体   English

ComboBox中的数据绑定:使用Usercontrol显示每个项目

[英]Databinding within ComboBox: Use Usercontrol to display each item

I have a model-class Person and a UserControl PersonComboBoxItem to display it. 我有一个模型类Person和一个UserControl PersonComboBoxItem来显示它。

What I'd like to do, is, creating a ComboBox where its ItemsSource is bound to my ObservableCollection<Person> called People and use my PersonUserControl to display each Person within the collection. 我想做的是,创建一个ComboBox ,其ItemsSource绑定到名为People的ObservableCollection<Person>并使用我的PersonUserControl来显示集合中的每个Person。

<Grid>
    <ComboBox SelectedIndex="0" ItemsSource="{Binding People}" >            
        <ComboBox.ItemTemplate>
            <DataTemplate>
                <local:PersonComboBoxItem Person="{Binding ###how do I get the current item here to set the property 'Person' on my PersonComboBoxItem class? ###  }"  />
            </DataTemplate>
        </ComboBox.ItemTemplate>
    </ComboBox>
</Grid>

I already worked my way thorugh this great article about databinding on msdn , but I couldn't get the transition over to my design-approach. 我已经按照这篇关于msdn上的数据绑定的伟大文章的方式工作了,但我无法过渡到我的设计方法。 Feel free to critisize it - I'm not sure, if this is the WPF-way to do it. 随意批评它 - 我不确定,如果这是WPF方式的话。

Regards, Florian 此致,弗洛里安

PS: My sample code can be downloaded from here . PS:我可以从这里下载我的示例代码。

Simply use an implicit DataTemplate to tell WPF how to draw the Person object when it encounters it in the visual tree 只需使用隐式DataTemplate告诉WPF如何在可视树中遇到Person对象时绘制它

<Grid>
    <ComboBox SelectedIndex="0" ItemsSource="{Binding People}" DisplayMemberPath="Name">            
        <ComboBox.Resources>
            <DataTemplate DataType="{x:Type local:Person}">
                <local:PersonComboBoxItem />
            </DataTemplate>
        </ComboBox.Resources>
    </ComboBox>
</Grid>

The ComboBox is already placing your Person data object in the VisualTree , and probably looks something like this: ComboBox已经将您的Person数据对象放在VisualTree ,可能看起来像这样:

<StackPanel>
    <ContentPresenter>
        <Person />
    </ContentPresenter>
    <ContentPresenter>
        <Person />
    </ContentPresenter>
    <ContentPresenter>
        <Person />
    </ContentPresenter>
    ...
</StackPanel>

So you are simply replacing where it says <Person /> with a <local:PersonComboBoxItem /> . 因此,您只需使用<local:PersonComboBoxItem />替换<Person />所在的位置。 The DataContext of your UserControl will be set to your Person object as well UserControlDataContext也将设置为Person对象

Also, the DataContext of the PersonItemComboBox will always be of type Person , so you won't even need the Person dependency property. 此外, PersonItemComboBoxDataContext将始终为Person类型,因此您甚至不需要Person依赖项属性。

The DataContext in the ItemTemplate is the current Person , to bind directly to the DataContext and thus to the Person just use {Binding} . ItemTemplateDataContext是当前的Person ,直接绑定到DataContext ,因此对Person只使用{Binding}

You could design your UserControl to directly use the current DataContext instead of a Person property, then you do not need to set anything explicitly. 您可以将UserControl设计为直接使用当前的DataContext而不是Person属性,然后您不需要显式设置任何内容。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM