簡體   English   中英

如何不在ListBoxItem選擇中包括Thumb

[英]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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM