簡體   English   中英

WPF列表框帶有分隔符,缺少突出顯示的顏色

[英]WPF Listbox with separators, missing highlighted colors

我有一個列表框(C#WPF),它在啟動時會被填充。 我想要在列表中的每個項目之間使用分隔符,並且我從另一個舊帖子中找到了此代碼,並且該代碼確實有效,但是當我使用該代碼時,我松開了突出顯示的顏色和選定的顏色,因此我不知道它在哪里出問題了。

如何找回突出顯示和選擇的顏色?

這是我正在使用的代碼。

<ListBox x:Name="radioBox" HorizontalAlignment="Left" Height="494" Margin="14,14,0,0" VerticalAlignment="Top" Width="287" Background="{x:Null}" FontFamily="Calibri" FontWeight="Thin" FontSize="25" Foreground="#FFEDEDF7" ScrollViewer.HorizontalScrollBarVisibility="Disabled" ScrollViewer.VerticalScrollBarVisibility="Disabled" BorderThickness="1" Padding="30,30,0,0" >
            <ListBox.ItemBindingGroup>
                <BindingGroup/>
            </ListBox.ItemBindingGroup>
            <ListBox.ItemContainerStyle>
                <Style TargetType="{x:Type ListBoxItem}">
                    <Setter Property="Template">
                        <Setter.Value>
                            <ControlTemplate TargetType="{x:Type ListBoxItem}">
                                <StackPanel>
                                    <Separator x:Name="Separator" Background="White" Opacity="0.1" Height="20"/>
                                    <ContentPresenter/>
                                </StackPanel>
                                <ControlTemplate.Triggers>
                                    <DataTrigger Binding="{Binding RelativeSource={RelativeSource PreviousData}}" Value="{x:Null}">
                                        <Setter Property="Visibility" TargetName="Separator" Value="Collapsed"/>
                                    </DataTrigger>
                                </ControlTemplate.Triggers>
                            </ControlTemplate>
                        </Setter.Value>
                    </Setter>
                </Style>
            </ListBox.ItemContainerStyle>
        </ListBox>

嘗試下面的代碼。 這是我完成的示例,應該可以使用。

<Window x:Class="ListBoxStyle.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:src="clr-namespace:ListBoxStyle"
    Title="MainWindow" Height="350" Width="525">
<Window.Resources>
    <Style x:Key="_ListBoxItemStyle" TargetType="ListBoxItem">
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="ListBoxItem">
                    <Border Name="_Border"
                            Padding="2"
                            SnapsToDevicePixels="true">
                        <ContentPresenter />
                    </Border>
                    <ControlTemplate.Triggers>
                        <Trigger Property="IsSelected" Value="true">
                            <Setter TargetName="_Border" Property="Background" Value="Yellow"/>
                            <Setter Property="Foreground" Value="Red"/>
                        </Trigger>
                    </ControlTemplate.Triggers>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>
</Window.Resources>
<Grid>
    <ListBox ItemContainerStyle="{DynamicResource _ListBoxItemStyle}"
             Width="200" Height="250"
             ScrollViewer.VerticalScrollBarVisibility="Auto"
             ScrollViewer.HorizontalScrollBarVisibility="Auto">
        <ListBoxItem>Hello</ListBoxItem>
        <ListBoxItem>Hi</ListBoxItem>
    </ListBox>
</Grid>

暫無
暫無

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

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