<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实际上是单个属性的模板,显示在TextBlock中.如果你设置:
<ListBox x:name="lstToys" Style="{DynamicResource ListBoxStyle1}" ItemsSource="{Binding Strings}" displayMemberPath="Toys"></ListBox>
它相当于:
<ListBox x:name="lstToys" Style="{DynamicResource ListBoxStyle1}" ItemsSource="{Binding Strings}"> <ListBox.ItemTemplate> <DataTemplate> <TextBlock Text="{Binding Toys}"/> </DataTemplate> </ListBox.ItemTemplate></ListBox>
您只需删除displayMemberPath路径并使用DataTemplate的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>
编辑
如果要设置工具提示但保留displayMemberPath,可以在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>
我建议不要这样做.请记住,使用displayMemberPath可以阻止数据模板中的任何复杂绑定.
总结以上是内存溢出为你收集整理的c#wpf – 无法同时设置DisplayMemberPath和ItemTemplate全部内容,希望文章能够帮你解决c#wpf – 无法同时设置DisplayMemberPath和ItemTemplate所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)