【问题标题】:WPF Listbox with separators, missing highlighted colors带有分隔符的 WPF 列表框,缺少突出显示的颜色
【发布时间】:2016-05-15 14:41:41
【问题描述】:

我有一个列表框 (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>

【问题讨论】:

    标签: c# wpf listbox separator


    【解决方案1】:

    试试下面的代码。这是我做的一个示例,它应该可以工作。

    <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>
    

    【讨论】:

    • 谢谢你的代码,但它没有任何不同,当分隔符在那里时,悬停或选择时没有颜色
    猜你喜欢
    • 2012-04-25
    • 2013-02-18
    • 1970-01-01
    • 2015-04-25
    • 2017-12-13
    • 2013-11-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多