c#wpf – 无法同时设置DisplayMemberPath和ItemTemplate

c#wpf – 无法同时设置DisplayMemberPath和ItemTemplate,第1张

概述我想在listboxItem中添加工具提示,但是当有DisplayMemberPath时它会启动问题.错误信息说:无法同时设置DisplayMemberPath和ItemTemplate.当我删除DisplayMemberPath时,每个列表项中的工具提示都有效.但我不想删除DisplayMemember因为我需要它.如何解决这个问题呢? <ListBox x:Name="lstToys" Sty 我想在ListBoxItem中添加工具提示,但是当有displayMemberPath时它会启动问题.错误信息说:无法同时设置displayMemberPath和ItemTemplate.当我删除displayMemberPath时,每个列表项中的工具提示都有效.但我不想删除displayMemember因为我需要它.如何解决这个问题呢?
<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所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/langs/1239878.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-06-06
下一篇 2022-06-06

发表评论

登录后才能评论

评论列表(0条)

保存