从 ListBox 的 ListBox 中移除高亮

Remove highlights from ListBox of ListBox

我有以下代码,无法去除高亮:

    <ListBox
      Name="OuterListBox"
      HorizontalAlignment="Center"
      VerticalAlignment="Center"
      AlternationCount="2"
      Background="White"
      BorderThickness="0"
      ItemsSource="{Binding Board}">
      <ListBox.ItemContainerStyle>
        <Style BasedOn="{StaticResource {x:Type ListBoxItem}}" TargetType="{x:Type ListBoxItem}">
          <Style.Resources>
            <AlternationConverter x:Key="AlternationPaddingConverter">
              <Thickness Right="25" />
              <Thickness Left="25" />
            </AlternationConverter>
          </Style.Resources>
          <Setter Property="Padding" Value="{Binding (ItemsControl.AlternationIndex), RelativeSource={RelativeSource Self}, Converter={StaticResource AlternationPaddingConverter}}" />
        </Style>
      </ListBox.ItemContainerStyle>
      <ListBox.ItemTemplate>
        <DataTemplate>
          <Grid>
            <ListBox
              Name="InnerListBox"
              Background="Transparent"
              BorderThickness="0"
              ItemContainerStyle="{StaticResource ChangeListBoxItemHighlight}"
              ItemsSource="{Binding}">
              <ListBox.ItemsPanel>
                <ItemsPanelTemplate>
                  <VirtualizingStackPanel Orientation="Horizontal" />
                </ItemsPanelTemplate>
              </ListBox.ItemsPanel>
              <ListBox.ItemTemplate>
                <DataTemplate>
                  <Ellipse
                    Margin="2"
                    Width="{Binding Size}"
                    Height="{Binding Size}"
                    Cursor="Hand"
                    Fill="{Binding Background}" />
                </DataTemplate>
              </ListBox.ItemTemplate>
            </ListBox>
          </Grid>
        </DataTemplate>
      </ListBox.ItemTemplate>
    </ListBox>

我尝试将 setter 与 Property="Template" 和值 <ControlTemplate TargetType="{x:Type ListBoxItem}"> 一起使用,但行的交替消失了。

如何删除高亮显示,但仍保留交替行?

控件模板定义了控件的视觉外观、它的状态和所需的部分。为了更改 Mouse OverFocused 等状态的表示,您必须修改控件模板。然而,这并不容易,因为控件模板很复杂并且很难从头开始构建。您始终可以参考不同控件的文档。

如您所见,需要考虑的因素很多,因此您最好 copying the default style and control template 并根据您的需要调整它们。我已经根据您的问题提取并改编了它们。从本质上讲,这意味着删除所有焦点和鼠标触发器并添加交替填充。

<Style x:Key="ListBoxItemStyle" TargetType="{x:Type ListBoxItem}">
   <Style.Resources>
      <AlternationConverter x:Key="AlternationPaddingConverter">
         <Thickness Right="25" />
         <Thickness Left="25" />
      </AlternationConverter>
   </Style.Resources>
   <Setter Property="Padding" Value="{Binding (ItemsControl.AlternationIndex), RelativeSource={RelativeSource Self}, Converter={StaticResource AlternationPaddingConverter}}" />
   <Setter Property="SnapsToDevicePixels" Value="True"/>
   <Setter Property="HorizontalContentAlignment" Value="{Binding HorizontalContentAlignment, RelativeSource={RelativeSource AncestorType={x:Type ItemsControl}}}"/>
   <Setter Property="VerticalContentAlignment" Value="{Binding VerticalContentAlignment, RelativeSource={RelativeSource AncestorType={x:Type ItemsControl}}}"/>
   <Setter Property="Background" Value="Transparent"/>
   <Setter Property="BorderBrush" Value="Transparent"/>
   <Setter Property="BorderThickness" Value="1"/>
   <Setter Property="Template">
      <Setter.Value>
         <ControlTemplate TargetType="{x:Type ListBoxItem}">
            <Border x:Name="Bd" Background="{TemplateBinding Background}" BorderThickness="{TemplateBinding BorderThickness}" BorderBrush="{TemplateBinding BorderBrush}" Padding="{TemplateBinding Padding}" SnapsToDevicePixels="true">
               <ContentPresenter HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}" SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}" VerticalAlignment="{TemplateBinding VerticalContentAlignment}"/>
            </Border>
            <ControlTemplate.Triggers>
               <Trigger Property="IsEnabled" Value="False">
                  <Setter Property="TextElement.Foreground" TargetName="Bd" Value="{DynamicResource {x:Static SystemColors.GrayTextBrushKey}}"/>
               </Trigger>
            </ControlTemplate.Triggers>
         </ControlTemplate>
      </Setter.Value>
   </Setter>
</Style>

然后您可以在 ListBox 中引用此样式,或者根据需要将其内联。

<ListBox
   Name="OuterListBox"
   HorizontalAlignment="Center"
   VerticalAlignment="Center"
   AlternationCount="2"
   Background="White"
   BorderThickness="0"
   ItemsSource="{Binding Board}"
   ItemContainerStyle="{StaticResource ListBoxItemStyle}">
   <ListBox.ItemTemplate>
      <DataTemplate>
         <Grid>
            <ListBox
               Name="InnerListBox"
               Background="Transparent"
               BorderThickness="0"
               ItemContainerStyle="{StaticResource ChangeListBoxItemHighlight}"
               ItemsSource="{Binding}">
               <ListBox.ItemsPanel>
                  <ItemsPanelTemplate>
                     <VirtualizingStackPanel Orientation="Horizontal" />
                  </ItemsPanelTemplate>
               </ListBox.ItemsPanel>
               <ListBox.ItemTemplate>
                  <DataTemplate>
                     <Ellipse
                        Margin="2"
                        Width="{Binding Size}"
                        Height="{Binding Size}"
                        Cursor="Hand"
                        Fill="{Binding Background}" />
                  </DataTemplate>
               </ListBox.ItemTemplate>
            </ListBox>
         </Grid>
      </DataTemplate>
   </ListBox.ItemTemplate>
</ListBox>