Wpf 移除ListBoxItems中的控件高亮显示,但不移除子控件
我正在使用列表框显示项目列表:PictureOrders。 我已经为列表框的项目应用了数据模板 我希望设置列表框的样式,以便当鼠标位于列表框中的任何项目上时,不会高亮显示这些项目,并且也不会高亮显示列表框中的选定项目 因此,我在列表框的资源中使用了以下内容:Wpf 移除ListBoxItems中的控件高亮显示,但不移除子控件,wpf,xaml,styles,Wpf,Xaml,Styles,我正在使用列表框显示项目列表:PictureOrders。 我已经为列表框的项目应用了数据模板 我希望设置列表框的样式,以便当鼠标位于列表框中的任何项目上时,不会高亮显示这些项目,并且也不会高亮显示列表框中的选定项目 因此,我在列表框的资源中使用了以下内容: <ListBox.Resources> <SolidColorBrush x:Key="{x:Static SystemColors.HighlightBrushKey}" Color="Tra
<ListBox.Resources>
<SolidColorBrush x:Key="{x:Static SystemColors.HighlightBrushKey}" Color="Transparent" />
<SolidColorBrush x:Key="{x:Static SystemColors.ControlBrushKey}" Color="Transparent" />
</ListBox.Resources>
以下是我当前的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>
10
15
20
25
50
100
150
5.
6.
7.
8.
9
10
您可以将
SolidColorBrush
资源设置为仅针对ListBoxItem
的样式
由于ComboBoxItem
继承自ListBoxItem
,因此ComboxBox仍将受到影响,因此还需要为ComboxItem
创建一个样式以应用默认颜色
<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>
您可以将
SolidColorBrush
资源设置为仅针对ListBoxItem
的样式
由于ComboBoxItem
继承自ListBoxItem
,因此ComboxBox仍将受到影响,因此还需要为ComboxItem
创建一个样式以应用默认颜色
<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>
在多次尝试解决这个问题后,我终于找到了一个有效的解决方案 我为ListBoxItem创建了一个样式,用于设置TemplateControl并设置鼠标悬停、选中和未选中状态的视觉状态 以下是该问题的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>
10
15
20
25
50
100
150
5.
6.
7.
8.
9
10
-Frinny在多次尝试解决这个问题后,我终于找到了一个有效的解决方案 我为ListBoxItem创建了一个样式,用于设置TemplateControl并设置鼠标悬停、选中和未选中状态的视觉状态 以下是该问题的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>
10
15
20
25
50
100
150
5.
6.
7.
8.
9
10
-Frinny这不是真的。ComboBox仍然受到影响哈哈,是的,因为ComboBoxItem继承自ListBoxItem。ComboBoxItem需要类似的样式才能将笔刷设置回默认颜色。很抱歉遗漏了这个问题。我可以看到你已经解决了这个问题,但是我仍然编辑了我的答案,这样其他人就不会对wro感到困惑了