C#WPF - 不能同时设置的DisplayMemberPath和ItemTemplate中 [英] c# wpf - cannot set both DisplayMemberPath and ItemTemplate
问题描述
我要在ListBoxItem中添加提示,但是当存在的DisplayMemberPath它启动的问题。错误消息说:不能同时设置的DisplayMemberPath和ItemTemplate中。当我删除的DisplayMemberPath,提示在每个列表项工作。但我不想因为我需要它来除去DisplayMemember。 ?如何解决这个问题。
< ListBox的X:名称=lstToys风格={DynamicResource ListBoxStyle1}的ItemsSource = {结合弦乐}的DisplayMemberPath =玩具MouseDoubleClick =lstToys_MouseDoubleClick>
< ListBox.ItemTemplate>
<&DataTemplate的GT;
< TextBlock的文本={结合}工具提示=这是一个提示/>
< / DataTemplate中>
< /ListBox.ItemTemplate>
< /列表框>
的DisplayMemberPath
是,实际上,对于一个单一属性的模板,在的TextBlock
所示。如果你设置:
< ListBox的X:名称=lstToys风格={DynamicResource ListBoxStyle1}
的ItemsSource ={结合弦乐}的DisplayMemberPath =玩具>
< /列表框>
这是等同于:
< ListBox的X:名称=lstToys风格={DynamicResource ListBoxStyle1}
的ItemsSource ={结合弦乐}>
< ListBox.ItemTemplate>
<&DataTemplate的GT;
< TextBlock的文本={结合玩具}/>
< / DataTemplate中>
< /ListBox.ItemTemplate>
< /列表框>
您可以简单地删除的DisplayMemberPath
路径
<$:在你的
的DataTemplate
绑定
使用值p $ p> < ListBox的X:名称=lstToys风格={DynamicResource ListBoxStyle1}
的ItemsSource ={结合弦乐}>
< ListBox.ItemTemplate>
<&DataTemplate的GT;
< TextBlock的文本={结合玩具}工具提示=这是一个提示!/>
< / DataTemplate中>
< /ListBox.ItemTemplate>
< /列表框>
修改
如果您想设置一个工具提示
,但保留的DisplayMemberPath
,您可以在<$ C $办呢C> ItemContainerStyle :
< ListBox的X:名称=lstToys风格={DynamicResource ListBoxStyle1}
的ItemsSource ={结合弦乐}的DisplayMemberPath =玩具>
< ListBox.ItemContainerStyle>
<风格的TargetType =ListBoxItem的>
< setter属性=工具提示VALUE =这里有一个提示!/>
< /样式和GT;
< /ListBox.ItemContainerStyle>
< /列表框>
我建议反对。请记住,使用的DisplayMemberPath
停止您从任何复杂的数据中的模板绑定。
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 DisplayMemember 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>
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.
这篇关于C#WPF - 不能同时设置的DisplayMemberPath和ItemTemplate中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!