c# wpf - cannot set both DisplayMemberPath and ItemTemplate
Asked Answered
P

1

10

I want to add tooltip in listboxItem but it starts problem when there is DisplayMemberPath. Error message said:

cannot set both DisplayMemberPath and ItemTemplate.

When I removed DisplayMemberPath, tooltip in each list item is working. But i dont want to remove DisplayMememberPath because I need it. How to solve this problem?

<ListBox x:Name="lstToys" Style="{DynamicResource ListBoxStyle1}"
            ItemsSource="{Binding Strings}" DisplayMemberPath="Toys"
            MouseDoubleClick="lstToys_MouseDoubleClick">
    <ListBox.ItemTemplate>
        <DataTemplate>
            <TextBlock Text="{Binding}" ToolTip="Here is a tooltip"/>
        </DataTemplate>
    </ListBox.ItemTemplate>
</ListBox>
Piccolo answered 16/8, 2013 at 12:32 Comment(1)
Put that path in the DataTemplate BindingWalloon
B
26

DisplayMemberPath is, in effect, a template for a single property, shown in a TextBlock. If you set:

<ListBox x:Name="lstToys" Style="{DynamicResource ListBoxStyle1}"  
         ItemsSource="{Binding Strings}" DisplayMemberPath="Toys">
</ListBox>

It is equivalent to:

<ListBox x:Name="lstToys" Style="{DynamicResource ListBoxStyle1}"  
         ItemsSource="{Binding Strings}">
    <ListBox.ItemTemplate>
        <DataTemplate>
            <TextBlock Text="{Binding Toys}"/>
        </DataTemplate>
    </ListBox.ItemTemplate>
</ListBox>

You can simply remove the DisplayMemberPath path and use the value in your DataTemplate's Binding:

<ListBox x:Name="lstToys" Style="{DynamicResource ListBoxStyle1}"  
         ItemsSource="{Binding Strings}">
    <ListBox.ItemTemplate>
        <DataTemplate>
            <TextBlock Text="{Binding Toys}" ToolTip="Here is a tooltip!"/>
        </DataTemplate>
    </ListBox.ItemTemplate>
</ListBox>

Edit

If you want to set a ToolTip but keep the DisplayMemberPath, you can do it at the ItemContainerStyle:

<ListBox x:Name="lstToys" Style="{DynamicResource ListBoxStyle1}"  
         ItemsSource="{Binding Strings}" DisplayMemberPath="Toys">
    <ListBox.ItemContainerStyle>
        <Style TargetType="ListBoxItem">
            <Setter Property="ToolTip" Value="Here's a tooltip!"/>
        </Style>
    </ListBox.ItemContainerStyle>
</ListBox>

I'd advise against it. Remember that use DisplayMemberPath stops you from any complex binding in your data template.

Blooded answered 16/8, 2013 at 12:36 Comment(4)
but i dont want to remove DisplayMemberPath because it is linked to some functions i created.Piccolo
@Piccolo See my edit. You still can't put an ItemTemplate if you have DisplayMemberPath. If you needs become more complex for templating, you will need to change your logic (I don't think using DisplayMemberPath for custom logic outside the view is a good idea).Blooded
worked for me too. thank you so much to save my time.Adherence
but this is not applied to selected value as after selection my combo box displays whole text and so it's width increase as it's auto. Any help for selected item too?Adherence

© 2022 - 2024 — McMap. All rights reserved.