WPF,如何设置ListView中项目的样式,例如选择项目时的背景颜色

问题描述 投票:0回答:1

当我选择一个项目时,我希望该项目的背景颜色是这种颜色#d3ddff而不是默认颜色https://imgur.com/n5KDzx6

当我将鼠标悬停在某个项目上时,我希望该项目的背景颜色为白色

单击该项目后,我单击具有“body 2”的文本框来输入内容,所选项目的背景颜色仍应为#d3ddff,而不是像这样的白色https://imgur.com/mFXxFDS另外,在图像中您还可以看到 | TextBox 的颜色是黑色的,如何将其设置为白色,以便用户知道他/她已经选择了 TextBox 或者正在输入?

此外,当我单击某个项目并按 ALT 键时,该项目周围现在有一个虚线轮廓,我该如何摆脱它?像这样https://imgur.com/WVuhQpt

这是我的 XAML 的完整源代码

<UserControl
    x:Class="Notes.WPF.Views.NotesListingView"
    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:dropdownmenucontrol="clr-namespace:DropdownMenuControl;assembly=DropdownMenuControl"
    xmlns:local="clr-namespace:Notes.WPF.Views"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    xmlns:whitedropdownmenucontrol="clr-namespace:WhiteDropdownMenuControl;assembly=WhiteDropdownMenuControl"
    d:DesignHeight="450"
    d:DesignWidth="800"
    mc:Ignorable="d">
    <Grid>
        <Grid.RowDefinitions>
            <RowDefinition Height="auto" />
            <RowDefinition Height="*" />
        </Grid.RowDefinitions>

        <Button
            Grid.Row="0"
            Height="40"
            Command="{Binding CreateCommand}"
            Content="Add a new note" />

        <Border
            Grid.Row="1"
            Margin="0,20,0,0"
            CornerRadius="5"
            SnapsToDevicePixels="True">
            <Grid>
                <Grid.OpacityMask>
                    <VisualBrush Visual="{Binding ElementName=Border}" />
                </Grid.OpacityMask>
                <Border
                    x:Name="Border"
                    Background="White"
                    CornerRadius="5" />

                <ListView
                    Grid.Row="1"
                    Background="#2b2d30"
                    BorderThickness="0"
                    ItemsSource="{Binding NotesListingItemViewModel}"
                    SelectedItem="{Binding SelectedNotesListingItemViewModel}">
                    <ListView.ItemContainerStyle>
                        <Style TargetType="ListViewItem">
                            <Setter Property="HorizontalContentAlignment" Value="Stretch" />
                            <Setter Property="BorderThickness" Value="0" />
                        </Style>
                    </ListView.ItemContainerStyle>
                    <ListView.ItemTemplate>
                        <DataTemplate>
                            <Border Padding="10">
                                <Grid>
                                    <Grid.ColumnDefinitions>
                                        <ColumnDefinition Width="*" />
                                        <ColumnDefinition Width="auto" />
                                    </Grid.ColumnDefinitions>

                                    <TextBlock Grid.Column="0" Text="{Binding Title}" />

                                    <whitedropdownmenucontrol:WhiteDropdownMenu Grid.Column="1">
                                        <StackPanel>
                                            <Button
                                                Width="200"
                                                Height="40"
                                                Padding="10"
                                                Content="MARKED AS COMPLETE" />
                                            <Button
                                                Width="200"
                                                Height="40"
                                                Padding="10"
                                                Content="MARKED AS IMPORTANT" />
                                            <Button
                                                Width="200"
                                                Height="40"
                                                Padding="10"
                                                Content="DELETE" />
                                        </StackPanel>
                                    </whitedropdownmenucontrol:WhiteDropdownMenu>
                                </Grid>
                            </Border>
                        </DataTemplate>
                    </ListView.ItemTemplate>
                </ListView>
            </Grid>

        </Border>
    </Grid>
</UserControl>

我在谷歌中找不到真正帮助我并解决我遇到的问题的方法。 ChatGPT 和 Google Bard 无法给出真正有帮助的答案

c# .net wpf xaml desktop-application
1个回答
0
投票

我希望我正确理解了这个问题,下面我制作了您需要放入 ItemContainerStyle 中的样式,我使用触发器来更改背景。

<Style TargetType="ListViewItem">
            <Setter Property="HorizontalContentAlignment"
                    Value="Stretch" />
            <Setter Property="BorderThickness"
                    Value="0" />
            <Setter Property="FocusVisualStyle"
                    Value="{x:Null}" />
            <Setter Property="Background" Value="Transparent"/>
            <Style.Triggers>
                <Trigger Property="IsMouseOver" Value="True">
                    <Setter Property="Background" Value="White"/>
                </Trigger>
                <Trigger Property="IsKeyboardFocusWithin"
                         Value="True">
                    <Setter Property="Background"
                            Value="White" />
                </Trigger>
                <Trigger Property="IsSelected"
                         Value="True">
                    <Setter Property="Background"
                            Value="#d3ddff" />
                </Trigger>
            </Style.Triggers>
        </Style>

“周围的虚线轮廓”是键盘聚焦时的样式,称为FocusVisualStyle,我在ListViewItem处将其设置为null。 但是 ItemTemplate 中有多个元素,您应该为所有这些元素设置 FocusVisualStyle = "{x:null}。

为了不影响 ListViewItem ,ItemTemplate 中的所有项目都设置 Background = "Transparent"。

© www.soinside.com 2019 - 2024. All rights reserved.