从 ListBox 的 ListBox 中删除突出显示

Posted

技术标签:

【中文标题】从 ListBox 的 ListBox 中删除突出显示【英文标题】:Remove highlights from ListBox of ListBox 【发布时间】:2021-07-16 22:00:37 【问题描述】:

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

    <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>

我尝试使用带有Property="Template" 和值&lt;ControlTemplate TargetType="x:Type ListBoxItem"&gt; 的setter,但行的交替消失了。

如何删除高光,但仍保留交替行?

【问题讨论】:

【参考方案1】:

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

ListBox Styles and Templates > ListBoxItem(部件和状态)

如您所见,需要考虑的因素很多,因此您最好使用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>

【讨论】:

非常感谢!它就像一个魅力!

以上是关于从 ListBox 的 ListBox 中删除突出显示的主要内容,如果未能解决你的问题,请参考以下文章

根据类中的值突出显示 ListBox 项

ListBox 选择时禁用突出显示 - Windows Phone 7

如何从 WPF 中的 ListBox 中删除边框?

在c#中删除listBoxItem时ListBox不刷新

Tkinter Listbox 以蓝色突出显示一个元素,但在另一个元素周围放置黑色边框?

VBA中如何使用ListBox控件呢?