gpt4 book ai didi

c# - WPF ListBoxItem 样式是否覆盖了我的 ItemTemplate?

转载 作者:行者123 更新时间:2023-12-03 10:35:30 24 4
gpt4 key购买 nike

我有一个绑定(bind)到列表 Items 的列表框具有两个属性的对象:NameIP .

我想为每个在悬停时显示 IP 地址的项目设置一个工具提示,所以我这样做了:

    <ListBox ItemsSource="{Binding Items}">
<ListBox.ItemTemplate>
<DataTemplate>
<TextBlock Text="{Binding Name}" ToolTip="{Binding IP}"/>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>

这行得通。但是,当我应用以下样式时,似乎我的 <DataTemplate>被完全忽略,列表只是调用 ToString()在我的对象上,工具提示根本不会出现。
<Style TargetType="ListBoxItem">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type ListBoxItem}">
<Border x:Name="Bd"
Background="{TemplateBinding Background}"
BorderBrush="{TemplateBinding BorderBrush}"
BorderThickness="{TemplateBinding BorderThickness}"
Padding="{TemplateBinding Padding}"
SnapsToDevicePixels="true">
<Grid>
<GridViewRowPresenter x:Name="gridrowPresenter"
Content="{TemplateBinding Property=ContentControl.Content}" />
<ContentPresenter x:Name="contentPresenter"
Content="{TemplateBinding Property=ContentControl.Content}" Visibility="Collapsed" />
</Grid>
</Border>
<ControlTemplate.Triggers>
<Trigger Property="GridView.ColumnCollection" Value="{x:Null}">
<Setter TargetName="contentPresenter" Property="Visibility" Value="Visible"/>
</Trigger>
<Trigger Property="Control.IsMouseOver" Value="true">
<Setter Property="Background" >
<Setter.Value>
<SolidColorBrush Color="#FFE6E6E6" Opacity="0.75"/>
</Setter.Value>
</Setter>
</Trigger>
<Trigger Property="IsSelected" Value="true">
<Setter Property="Background" >
<Setter.Value>
<SolidColorBrush Color="#FFA5C5D1" Opacity="0.75"/>
</Setter.Value>
</Setter>
<Setter Property="Foreground" Value="Black"/>
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>

为什么这种样式会阻止应用 DataTemplate?

最佳答案

您是否尝试过不绑定(bind) ContentPresenter 的 Content ?默认情况下,它应该可以在没有它的情况下工作。绑定(bind)它(或使用 TemplateBinding)可能会破坏默认的 ContentControl 功能。

查看 ListBoxItem默认模板:msdn.microsoft.com/es-es/library/ms750821(v=vs.85).aspx
ContentPresenter保持原样,剩下的就是 ContentControl 逻辑。

关于c# - WPF ListBoxItem 样式是否覆盖了我的 ItemTemplate?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31274204/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com