Remove Control Highlight From ListBoxItems but not

2019-09-04 09:15发布

I am using a ListBox to display a list of items: PictureOrders. I have applied a DataTemplate for the ListBox's Items.

I would like to style the list box so that the items are not highlighted when the mouse is over any of the items in the list box and so that the selected item in the list box is not highlighted either.

So, I used the following in the ListBox's resources:

  <ListBox.Resources>
            <SolidColorBrush x:Key="{x:Static SystemColors.HighlightBrushKey}" Color="Transparent"  />
            <SolidColorBrush x:Key="{x:Static SystemColors.ControlBrushKey}" Color="Transparent" />
   </ListBox.Resources>

However, now the ComboBoxes within the ListBox no longer have highlight colors which poses a bit of a problem.

I have the following classes:

Public Class PictureOrder
   Public Property OrderName As String
   Public Property NumberOfPictures As Integer
   Public Property QualityOfPictures As Integer
   Public Property Comments As String
End Class

Public Class PictureOrders
   Public Property PictureOrders As ObjectModel.ObservableCollection(Of PictureOrder)

   Public Sub New()
    PictureOrders = New ObjectModel.ObservableCollection(Of PictureOrder)
    For i As Integer = 1 To 11 '
        Dim picOrder As New PictureOrder With {.OrderName = String.Format("Picture Order # {0}", i.ToString),
                                               .NumberOfPictures = 50,
                                               .QualityOfPictures = 10,
                                               .Comments = String.Format("Picture Order # {0} Comments", i.ToString)}
        PictureOrders.Add(picOrder)
    Next
   End Sub
End Class

Here is my current XAML:

<Window x:Class="Window1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="Picture Orders" Height="600" Width="600"
xmlns:myProj="clr-namespace:TryingWPF">
<Window.Resources>
    <x:Array x:Key="NumberOfPicturesOptions" Type="sys:Int32"
             xmlns:sys="clr-namespace:System;assembly=mscorlib">
        <sys:Int32>10</sys:Int32>
        <sys:Int32>15</sys:Int32>
        <sys:Int32>20</sys:Int32>
        <sys:Int32>25</sys:Int32>
        <sys:Int32>50</sys:Int32>
        <sys:Int32>100</sys:Int32>
        <sys:Int32>150</sys:Int32>
    </x:Array>
    <x:Array x:Key="QualityOfPicturesOptions" Type="sys:Int32"
              xmlns:sys="clr-namespace:System;assembly=mscorlib">
        <sys:Int32>5</sys:Int32>
        <sys:Int32>6</sys:Int32>
        <sys:Int32>7</sys:Int32>
        <sys:Int32>8</sys:Int32>
        <sys:Int32>9</sys:Int32>
        <sys:Int32>10</sys:Int32>
    </x:Array>
    <myProj:PictureOrders x:Key="Orders" />
</Window.Resources>
<ListBox x:Name="OrderListings" DataContext="{StaticResource Orders}" ItemsSource="{Binding PictureOrders}"  SelectedIndex="0">
    <ListBox.Resources>
        <SolidColorBrush x:Key="{x:Static SystemColors.HighlightBrushKey}" Color="Transparent"  />
        <SolidColorBrush x:Key="{x:Static SystemColors.ControlBrushKey}" Color="Transparent" />
    </ListBox.Resources>
    <ListBox.ItemTemplate>
        <DataTemplate>
            <Expander x:Name="PhotoOrderExpander"
                      Content="{Binding}" 
                      Header="{Binding OrderName}"
                      IsExpanded="{Binding Path=IsSelected, RelativeSource={RelativeSource AncestorType={x:Type ListBoxItem}}}">
                <Expander.ContentTemplate>
                    <DataTemplate>
                        <DockPanel Margin="25,5">
                            <DockPanel DockPanel.Dock="Top">
                                <Label VerticalAlignment="Top" Content="Order Name" />
                                <TextBox Text="{Binding OrderName, ValidatesOnExceptions=True}" VerticalAlignment="Top" MaxLength="50"/>
                            </DockPanel>
                            <DockPanel DockPanel.Dock="Top">
                                <Label Content="NumberOfPictures" />
                                <ComboBox ItemsSource="{Binding Source={StaticResource NumberOfPicturesOptions}}" 
                                          SelectedItem="{Binding Path=NumberOfPictures, ValidatesOnExceptions=True}" />
                            </DockPanel>
                            <DockPanel DockPanel.Dock="Top">
                                <Label Content="Quality Of Pictures" />
                                <ComboBox ItemsSource="{StaticResource QualityOfPicturesOptions}" 
                                          SelectedItem="{Binding Path=QualityOfPictures, ValidatesOnExceptions=True}" />
                            </DockPanel>
                            <DockPanel DockPanel.Dock="Top">
                                <Label Content="Comments" />
                                <TextBox Text="{Binding Comments}" />
                            </DockPanel>
                        </DockPanel>
                    </DataTemplate>
                </Expander.ContentTemplate>
            </Expander>
        </DataTemplate>
    </ListBox.ItemTemplate>
</ListBox>

标签: wpf xaml styles
2条回答
啃猪蹄的小仙女
2楼-- · 2019-09-04 09:28

After many different attempts to solve this problem, I finally came to a solution that works.

I created a style for the ListBoxItem that set the TemplateControl and set the VisualStates for the MouseOver, Selected, and UnSelected states.

Here is the XAML code solution to the problem:

  <Window.Resources>

    <Style TargetType="ListBoxItem" x:Key="ListBoxWithNoSelection">
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="ListBoxItem">
                    <Border x:Name="LayoutRoot" 
                        BorderBrush="{TemplateBinding BorderBrush}" 
                        BorderThickness="{TemplateBinding BorderThickness}" 
                        Background="{TemplateBinding Background}" 
                        HorizontalAlignment="{TemplateBinding HorizontalAlignment}" 
                        VerticalAlignment="{TemplateBinding VerticalAlignment}">
                        <VisualStateManager.VisualStateGroups>
                            <VisualStateGroup x:Name="CommonStates">
                                <VisualState x:Name="Normal" />
                                <VisualState x:Name="MouseOver" />
                                <VisualState x:Name="Disabled" />
                            </VisualStateGroup>
                            <VisualStateGroup x:Name="SelectionStates">
                                <VisualState x:Name="Unselected" />
                                <VisualState x:Name="Selected" />
                            </VisualStateGroup>
                        </VisualStateManager.VisualStateGroups>

                        <ContentControl x:Name="ContentContainer"
                                        ContentTemplate="{TemplateBinding ContentTemplate}" 
                                        Content="{TemplateBinding Content}" Foreground="{TemplateBinding Foreground}" 
                                        HorizontalContentAlignment="{TemplateBinding HorizontalContentAlignment}" 
                                        Margin="{TemplateBinding Padding}" 
                                        VerticalContentAlignment="{TemplateBinding VerticalContentAlignment}"/>

                    </Border>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>

    <x:Array x:Key="NumberOfPicturesOptions" Type="sys:Int32"
             xmlns:sys="clr-namespace:System;assembly=mscorlib">
        <sys:Int32>10</sys:Int32>
        <sys:Int32>15</sys:Int32>
        <sys:Int32>20</sys:Int32>
        <sys:Int32>25</sys:Int32>
        <sys:Int32>50</sys:Int32>
        <sys:Int32>100</sys:Int32>
        <sys:Int32>150</sys:Int32>
    </x:Array>
    <x:Array x:Key="QualityOfPicturesOptions" Type="sys:Int32"
              xmlns:sys="clr-namespace:System;assembly=mscorlib">
        <sys:Int32>5</sys:Int32>
        <sys:Int32>6</sys:Int32>
        <sys:Int32>7</sys:Int32>
        <sys:Int32>8</sys:Int32>
        <sys:Int32>9</sys:Int32>
        <sys:Int32>10</sys:Int32>
    </x:Array>
    <myProj:PictureOrders x:Key="Orders" />
</Window.Resources>


<ListBox x:Name="OrderListings" DataContext="{StaticResource Orders}" 
         ItemsSource="{Binding PictureOrders}"  
         SelectedIndex="0"
         ItemContainerStyle="{StaticResource ListBoxWithNoSelection}">
    <ListBox.ItemTemplate>
        <DataTemplate>
            <Expander x:Name="PhotoOrderExpander"
                      Content="{Binding}" 
                      Header="{Binding OrderName}"
                      IsExpanded="{Binding Path=IsSelected, RelativeSource={RelativeSource AncestorType={x:Type ListBoxItem}}}">
                <Expander.ContentTemplate>
                    <DataTemplate>
                        <DockPanel Margin="25,5">
                            <DockPanel DockPanel.Dock="Top">
                                <Label VerticalAlignment="Top" Content="Order Name" />
                                <TextBox Text="{Binding OrderName, ValidatesOnExceptions=True}" VerticalAlignment="Top" MaxLength="50"/>
                            </DockPanel>
                            <DockPanel DockPanel.Dock="Top">
                                <Label Content="NumberOfPictures" />
                                <ComboBox ItemsSource="{Binding Source={StaticResource NumberOfPicturesOptions}}" 
                                          SelectedItem="{Binding Path=NumberOfPictures, ValidatesOnExceptions=True}" />
                            </DockPanel>
                            <DockPanel DockPanel.Dock="Top">
                                <Label Content="Quality Of Pictures" />
                                <ComboBox ItemsSource="{StaticResource QualityOfPicturesOptions}" 
                                          SelectedItem="{Binding Path=QualityOfPictures, ValidatesOnExceptions=True}" />
                            </DockPanel>
                            <DockPanel DockPanel.Dock="Top">
                                <Label Content="Comments" />
                                <TextBox Text="{Binding Comments}" />
                            </DockPanel>
                        </DockPanel>
                    </DataTemplate>
                </Expander.ContentTemplate>
            </Expander>
        </DataTemplate>
    </ListBox.ItemTemplate>
</ListBox>

-Frinny

查看更多
干净又极端
3楼-- · 2019-09-04 09:49

You can set the SolidColorBrush resources in a style that only targets ListBoxItem.

Since ComboBoxItem inherits from ListBoxItem, the ComboBox will still be affected though, so it's also necessary to create a style for ComboBoxItem to apply the default colors.

<ListBox.Resources>
    <Style TargetType="ListBoxItem">
        <Style.Resources>
            <SolidColorBrush x:Key="{x:Static SystemColors.ControlBrushKey}" Color="Transparent" />
            <SolidColorBrush x:Key="{x:Static SystemColors.HighlightBrushKey}" Color="Transparent" />
        </Style.Resources>
    </Style>

    <Style TargetType="ComboBoxItem">
        <Style.Resources>
            <SolidColorBrush x:Key="{x:Static SystemColors.HighlightBrushKey}" Color="{x:Static SystemColors.HighlightColor}"/>
             <SolidColorBrush x:Key="{x:Static SystemColors.ControlBrushKey}" Color="{x:Static SystemColors.ControlColor}"/>
        </Style.Resources>
    </Style>
</ListBox.Resources>
查看更多
登录 后发表回答