【问题标题】:Remove Control Highlight From ListBoxItems but not children controls从 ListBoxItems 中删除控件突出显示,但不删除子控件
【发布时间】:2012-11-15 21:39:57
【问题描述】:

我正在使用 ListBox 来显示项目列表:PictureOrders。 我已经为 ListBox 的项目应用了 DataTemplate。

我想设置列表框的样式,以便当鼠标悬停在列表框中的任何项目上时项目不会突出显示,并且列表框中的选定项目也不会突出显示。

所以,我在 ListBox 的资源中使用了以下内容:

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

但是,现在 ListBox 中的 ComboBoxes 不再具有高亮颜色,这会带来一些问题。

我有以下课程:

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

这是我当前的 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


    【解决方案1】:

    您可以将SolidColorBrush 资源设置为仅针对ListBoxItem 的样式。

    由于ComboBoxItem 继承自ListBoxItem,ComboBox 仍然会受到影响,因此还需要为ComboBoxItem 创建样式以应用默认颜色。

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

    【讨论】:

    • 这不是真的。组合框仍然受到影响
    • 哈哈,是的,因为 ComboBoxItem 继承自 ListBoxItem。您需要 ComboBoxItem 的类似样式将画笔设置回默认颜色。很抱歉错过了那个。
    • 我可以看到你已经解决了问题,但我仍然编辑了我的答案,所以其他人不会对我先写的错误内容感到困惑。
    • 我尝试了您的解决方案,但它似乎可以正常工作,只是 ListBoxItem 中 TextBlocks 的前景改变了颜色。我确信可以使用相同的方法解决此问题。
    【解决方案2】:

    在多次尝试解决这个问题后,我终于找到了一个可行的解决方案。

    我为 ListBoxItem 创建了一个样式,用于设置 TemplateControl 并为 MouseOver、Selected 和 UnSelected 状态设置 VisualStates。

    这里是该问题的 XAML 代码解决方案:

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

    -弗林尼

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-06-17
      • 1970-01-01
      • 2023-03-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-08-31
      • 1970-01-01
      相关资源
      最近更新 更多