[英]How to change selection highlight colour of a ListBoxItem in UWP?
[英]How not to include a Thumb in ListBoxItem selection
我有一些物品。 我正在使用DataTemplate
在WPF ListBox中顯示項目。 DataTemplate的一部分是Thumb
,用於調整大小並在視覺上分離項目之間。 Thumb.Visibility
綁定到項目的屬性(例如,最后一個項目沒有可見的Thumb)。
問題在於,在ListBox
中選擇一個項目也會同時選擇Thumb
,因為它是ListBoxItem
一部分。 所需的行為是僅選擇沒有Thumb
的數據。
如何獲得這種行為? 我不想通過ListBox
代碼添加項目,也不想手動處理Thumb
的可見性。 目前,我從DataTemplate
獲得所有這些信息。
在項目容器樣式(而不是DataTemplate)中執行此操作。 即定義您自己的ListBoxItem
樣式,並僅突出顯示所需內容。 例如:
<Style x:Key="MyStyle" TargetType="{x:Type ListBoxItem}">
<Setter Property="SnapsToDevicePixels" Value="true"/>
<Setter Property="OverridesDefaultStyle" Value="true"/>
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type ListBoxItem}">
<Grid>
<Border
Name="Border"
Margin="0, 0, 0, 1"
Padding="2"
SnapsToDevicePixels="true">
<ContentPresenter />
</Border>
<Border Grid.Row="1" BorderThickness="0, 0, 0, 1" BorderBrush="Black"
SnapsToDevicePixels="True"/>
</Grid>
<ControlTemplate.Triggers>
<Trigger Property="IsSelected" Value="true">
<Setter TargetName="Border" Property="Background" Value="#DDDDDD"/>
</Trigger>
<Trigger Property="IsEnabled" Value="false">
<Setter Property="Foreground" Value="#888888"/>
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
接着:
<ListBox ItemContainerStyle="{StaticResource MyStyle}">
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.