Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Compound DisplayMemberPath for a combobox

Tags:

c#

combobox

I need to create a DisplayMemberPath that is a compound of a few properties (ie object.category.Name+" -> "+object.description) I'm pretty sure I can do this by creating a dynamic data type that encapsulates the object and also adds a new property called displayField that is what I need but I'm wondering if there is a more proper way to do this that does not involve creating a new object. Any ideas?

like image 965
dregan Avatar asked Dec 28 '22 13:12

dregan


1 Answers

DisplayMemberPath is just a "shortcut" for when you don't need a complex template for items. If you need more control, use ItemTemplate instead:

<ComboBox ItemsSource="{Binding Items}">
    <ComboBox.ItemTemplate>
        <DataTemplate>
            <TextBlock>
                <TextBlock.Text>
                    <MultiBinding StringFormat="{}{0} -> {1}">
                        <Binding Path="Category.Name" />
                        <Binding Path="Description" />
                    </MultiBinding>
                </TextBlock.Text>
            </TextBlock>
        </DataTemplate>
    </ComboBox.ItemTemplate>
</ComboBox>
like image 141
Thomas Levesque Avatar answered Jan 08 '23 08:01

Thomas Levesque