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

tzcvj98z  于 12个月前  发布在  其他
关注(0)|答案(1)|浏览(108)

当我选择一个项目时,我希望该项目的背景色是此颜色#d3ddff,而不是默认颜色https://imgur.com/n5KDzx6
当我悬停在一个项目,我希望该项目的背景颜色只是白色
点击项目后,我点击文本框,有“身体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不能给予实际有帮助的答案

5jvtdoz2

5jvtdoz21#

我希望我正确理解了这个问题,下面我做了你需要放在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>

“dotted outline around”是用键盘聚焦时的样式,称为FocusVisualStyle,我在ListViewItem处将其设置为null。但是ItemTemplate中有几个元素,应该为所有元素设置FocusVisualStyle =“{x:null}。
为了不影响ListViewItem,ItemTemplate中的所有项都设置Background =“Transparent”。

相关问题