【问题标题】:WPF MetroWindow Listbox remove Focus ColorWPF MetroWindow 列表框删除焦点颜色
【发布时间】:2019-01-10 08:03:21
【问题描述】:

我搜索了很长时间来寻找解决方案,但由于我没有找到任何有用的东西,所以我在问我自己的问题。 我在 WPF 中使用 MahApps Metro,它支持平铺视图。我有一些应用于磁贴的图片,磁贴本身存储到一个列表框中,所以它看起来有点像 Windows 10 开始菜单。但是每当我将鼠标悬停在磁贴项目上时,listBox 焦点颜色就会变得可见

因为这些是磁贴,而列表框焦点对我来说不需要,所以蓝色的周围对我来说看起来很丑,但我还没有找到禁用它的方法。 我的 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>

我需要做些什么才能将焦点颜色至少设置为“白色”,这样它就不再可见了

【问题讨论】:

标签: c# wpf listbox


【解决方案1】:

您需要通过表达式混合获取标准 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}">

【讨论】:

    猜你喜欢
    • 2012-03-31
    • 2018-04-14
    • 1970-01-01
    • 2011-03-07
    • 2010-10-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-08-25
    相关资源
    最近更新 更多