簡體   English   中英

樣式選定的ListBoxItem

[英]Styling a Selected ListBoxItem

編輯:

<Window x:Class="test_project.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:test_project"
        mc:Ignorable="d"
        Title="MainWindow" Height="350" Width="525">

    <Grid>
        <ListBox>
            <ListBox.Resources>
                <SolidColorBrush x:Key="{x:Static SystemColors.HighlightBrushKey}" Color="Red" />
                <SolidColorBrush x:Key="{x:Static SystemColors.HighlightTextBrushKey}" Color="White" />

                <SolidColorBrush x:Key="{x:Static SystemColors.InactiveSelectionHighlightBrushKey}" Color="Yellow" Opacity="0.6" />
                <SolidColorBrush x:Key="{x:Static SystemColors.InactiveSelectionHighlightTextBrushKey}" Color="White" />
            </ListBox.Resources>
            <ListBox.Items>
                <ListBoxItem Content="Hello"/>
                <ListBoxItem Content="Hello"/>
            </ListBox.Items>
        </ListBox>
    </Grid>
</Window>

我想設置一個ListBoxItem樣式,以便在選中時具有白色文本的藍色背景。 我在網上查看了許多答案,但它們似乎都提出了相互矛盾的意見,到目前為止,這些意見都沒有為我解決。 這是我的ListBox

<ListBox Grid.Row="1" Margin="5" ItemContainerStyle="{StaticResource BlueItemStyle}"
            BorderBrush="#06658D" 
            ItemsSource="{Binding UsersView}">
    <ListBox.ItemTemplate>
        <DataTemplate>
            <TextBlock Text="{Binding Name}"/>
        </DataTemplate>
    </ListBox.ItemTemplate>
</ListBox>

到目前為止,這是我使用過的Style,但沒有實現:

<!--ListBoxItem-->
<Style TargetType="{x:Type ListBoxItem}" x:Key="BlueItemStyle">
    <Setter Property="Height" Value="40"/>
    <Style.Triggers>
        <Trigger Property="IsSelected" Value="True">
            <Setter Property="Foreground" Value="White"/>
            <Setter Property="Background" Value="#06658D"/>
        </Trigger>
    </Style.Triggers>
</Style>

但是,這不會以任何方式設置ListBoxItem樣式。 簡而言之,我的問題將是為選定的ListBoxItem設置樣式的最簡單方法,因此ListBox如下所示:

在此處輸入圖片說明

該解決方案在Windows 10上不起作用,這意味着它不再適用。 謝謝,微軟。

據我所知,這將意味着必須替換ListBoxItem控件模板。 解決該問題的兩個問題:

  1. WPF。 ListBox項目樣式
  2. https://stackoverflow.com/a/35810145/424129

您不能以這種方式更改所選項目的背景顏色; ListBoxItemBackground屬性不會更改選擇狀態。 相反, ListBoxItem的默認控件模板未選中使用Background ,並具有一個觸發器,當IsSelectedtrue時,該觸發器會用{DynamicResource {x:Static SystemColors.HighlightBrushKey}}替換某些模板子級的實際背景畫筆。

可以DataTemplate的子項執行相同的操作,也可以替換Template但僅重寫資源會更容易。

您也可以全局覆蓋同一資源,以獲得一致的外觀。

<ListBox 
    Grid.Row="1" 
    Margin="5" 
    ItemContainerStyle="{StaticResource BlueItemStyle}"
    BorderBrush="#06658D" 
    ItemsSource="{Binding UsersView}"
    >
    <ListBox.Resources>
        <SolidColorBrush x:Key="{x:Static SystemColors.HighlightBrushKey}" Color="#06658D" />
        <SolidColorBrush x:Key="{x:Static SystemColors.HighlightTextBrushKey}" Color="White" />

        <!-- 
        The default inactive selection color in Win7 is much too pale for my taste; 
        our older users are unable to distinguish it from white on most monitors. 
        -->
        <SolidColorBrush x:Key="{x:Static SystemColors.InactiveSelectionHighlightBrushKey}" Color="#06658D" Opacity="0.6" />
        <SolidColorBrush x:Key="{x:Static SystemColors.InactiveSelectionHighlightTextBrushKey}" Color="White" />
    </ListBox.Resources>
    <ListBox.ItemTemplate>
        <DataTemplate>
            <TextBlock Text="{Binding Name}"/>
        </DataTemplate>
    </ListBox.ItemTemplate>
</ListBox>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM