WPF MetroWindow列表框删除焦点颜色

时间:2019-01-10 08:03:21

标签: c# wpf listbox

我花了很长时间搜索解决方案,但是由于找不到任何有用的信息,我在问自己的问题。 我在WPF中使用MahApps Metro,它支持Tile View。我有一些图片已应用到图块,并且图块本身已存储在ListBox中,所以它有点像Windows 10的“开始”菜单。但是每当我将鼠标悬停在磁贴项目上时,listBox焦点颜色就会变得可见

enter image description here

由于这些是图块,并且列表框焦点不需要我,蓝色的周围看起来对我来说很难看,但是我没有找到禁用它的方法。 我的XAML代码非常节俭:

<Controls:MetroWindow x:Class="Berichtsheft_Analyzer.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    xmlns:local="clr-namespace:Berichtsheft_Analyzer"
    xmlns:Controls="clr-namespace:MahApps.Metro.Controls;assembly=MahApps.Metro"
    mc:Ignorable="d"
    Title="Report Portfolio Analyzer" 
    Height="700"
    Width="900"
    Icon="C:\Users\lerchers\Desktop\testbilder BCS\moustache.png">

<Grid Name="Grid1">
    <Grid.RowDefinitions>
        <RowDefinition />
        <RowDefinition  Height="188"/>
    </Grid.RowDefinitions>
    <ListBox Name="Hans" ScrollViewer.HorizontalScrollBarVisibility="Disabled" >
        <ListBox.ItemsPanel>
            <ItemsPanelTemplate>
                <WrapPanel />
            </ItemsPanelTemplate>
        </ListBox.ItemsPanel>
        <ListBox.ItemTemplate>
            <DataTemplate>
                <StackPanel Margin="15,10">

                    <Controls:Tile Title="{Binding Path=_Name}" Background="Brown" TiltFactor="0" Width="225" Height="225">
                        <Rectangle Width="160" Height="160" >
                            <Rectangle.Fill>
                                <ImageBrush ImageSource="{Binding Path=_Imagesrc}"/>
                            </Rectangle.Fill>
                        </Rectangle>
                    </Controls:Tile>
                </StackPanel>
            </DataTemplate>
        </ListBox.ItemTemplate>
    </ListBox>
</Grid>

我需要怎么做才能将焦点颜色至少设置为“白色”,从而使其不再可见

1 个答案:

答案 0 :(得分:0)

您需要通过表达式blend获取标准ListView的ItemContainerStyle的副本,并删除设置容器边框的触发器。

<Style x:Key="ListBoxItemStyle1" TargetType="{x:Type ListBoxItem}">
    <Setter Property="SnapsToDevicePixels" Value="True"/>
    <Setter Property="Padding" Value="4,1"/>
    <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="FocusVisualStyle" Value="{StaticResource FocusVisual}"/>
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="{x:Type ListBoxItem}">
                <Border x:Name="Bd" BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}" Background="{TemplateBinding Background}" Padding="{TemplateBinding Padding}" SnapsToDevicePixels="true">
                    <ContentPresenter HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}" SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}" VerticalAlignment="{TemplateBinding VerticalContentAlignment}"/>
                </Border>
                <ControlTemplate.Triggers>
                    <MultiTrigger>
                        <MultiTrigger.Conditions>
                            <Condition Property="IsMouseOver" Value="True"/>
                        </MultiTrigger.Conditions>
                        <Setter Property="Background" TargetName="Bd" Value="{StaticResource Item.MouseOver.Background}"/>
                        <!-- change this one to white? -->
                        <Setter Property="BorderBrush" TargetName="Bd" Value="{StaticResource Item.MouseOver.Border}"/>
                    </MultiTrigger>
                    <MultiTrigger>
                        <MultiTrigger.Conditions>
                            <Condition Property="Selector.IsSelectionActive" Value="False"/>
                            <Condition Property="IsSelected" Value="True"/>
                        </MultiTrigger.Conditions>
                        <Setter Property="Background" TargetName="Bd" Value="{StaticResource Item.SelectedInactive.Background}"/>
                        <Setter Property="BorderBrush" TargetName="Bd" Value="{StaticResource Item.SelectedInactive.Border}"/>
                    </MultiTrigger>
                    <MultiTrigger>
                        <MultiTrigger.Conditions>
                            <Condition Property="Selector.IsSelectionActive" Value="True"/>
                            <Condition Property="IsSelected" Value="True"/>
                        </MultiTrigger.Conditions>
                        <Setter Property="Background" TargetName="Bd" Value="{StaticResource Item.SelectedActive.Background}"/>
                        <!-- change this one to white? -->
                        <Setter Property="BorderBrush" TargetName="Bd" Value="{StaticResource Item.SelectedActive.Border}"/>
                    </MultiTrigger>
                    <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 Name="Hans" ScrollViewer.HorizontalScrollBarVisibility="Disabled" 
ItemContainerStyle="{StaticResource ListBoxItemStyle1}">