从 ListBoxItems 中删除控件突出显示,但不从子控件中删除 [英] Remove Control Highlight From ListBoxItems but not children controls

查看:24
本文介绍了从 ListBoxItems 中删除控件突出显示,但不从子控件中删除的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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.

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

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>

然而,现在 ListBox 中的 ComboBoxes 不再有高亮颜色,这会带来一些问题.

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

我有以下课程:

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:

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>

推荐答案

经过多次不同的尝试来解决这个问题,我终于找到了一个有效的解决方案.

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

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

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

以下是该问题的 XAML 代码解决方案:

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>

-弗林妮

这篇关于从 ListBoxItems 中删除控件突出显示,但不从子控件中删除的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆