How to remove the left-hand blue line on custom se

2019-08-02 14:08发布

问题:

I found this code which replaces the default select style with a custom style for the select ListBoxItem in a ListBox. However, there is still a little blue line on the left from the default style which I can't remove with any padding or margin changes.

How can I remove that blue line and completely determine the style of the selected ListBoxItem?

alt text http://tanguay.info/web/external/blueLineLeft.png

<Window x:Class="CodingContext.Window1"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:local="clr-namespace:CodingContext"
    Title="Window1" Height="300" Width="300">
    <Window.Resources>

        <DataTemplate x:Key="ItemTemplate" DataType="{x:Type local:Person}">
            <Grid>
                <Grid.ColumnDefinitions>
                    <ColumnDefinition Width="*"/>
                    <ColumnDefinition Width="Auto" />
                </Grid.ColumnDefinitions>

                <TextBlock Grid.Column="0" Text="{Binding Path=Name, StringFormat=Name: \{0\}}" />
                <TextBlock Grid.Column="1" Text="{Binding Path=Age, StringFormat=Age: \{0\}}" />
            </Grid>
        </DataTemplate>

        <DataTemplate x:Key="SelectedTemplate" DataType="{x:Type local:Person}">
            <Grid Background="Yellow">

                <Grid.ColumnDefinitions>
                    <ColumnDefinition Width="*"/>
                    <ColumnDefinition Width="Auto" />
                </Grid.ColumnDefinitions>
                <Grid.RowDefinitions>
                    <RowDefinition />
                    <RowDefinition />
                    <RowDefinition />
                    <RowDefinition />
                </Grid.RowDefinitions>

                <TextBlock Foreground="Black" Grid.Row="0" Grid.Column="0" Text="Person" FontSize="14" FontWeight="Bold" />
                <TextBlock Grid.Row="1" Grid.Column="0" Text="{Binding Path=Name, StringFormat=Name: \{0\}}" />
                <TextBlock Grid.Row="1" Grid.Column="1" Text="{Binding Path=Age, StringFormat=Age: \{0\}}" />

                <TextBlock Grid.Row="2" Grid.Column="0" Text="Address" FontSize="14" FontWeight="Bold" />
                <StackPanel Grid.Row="3" Grid.Column="0" Grid.ColumnSpan="2" Orientation="Horizontal">
                    <TextBlock Text="{Binding Address}" />
                    <TextBlock Text="{Binding City, StringFormat= \{0\}}" />
                    <TextBlock Text="{Binding State, StringFormat= \{0\}}" />
                    <TextBlock Text="{Binding Zip, StringFormat= \{0\}}" />
                </StackPanel>
            </Grid>
        </DataTemplate>

        <Style TargetType="{x:Type ListBoxItem}" x:Key="ContainerStyle">
            <Setter Property="ContentTemplate" Value="{StaticResource ItemTemplate}" />
            <Setter Property="Margin" Value="5" />
            <Style.Triggers>
                <Trigger Property="IsSelected" Value="True">
                    <Setter Property="ContentTemplate" Value="{StaticResource SelectedTemplate}" />
                </Trigger>
            </Style.Triggers>
        </Style>

    </Window.Resources>

    <ListBox HorizontalContentAlignment="Stretch" Margin="10" x:Name="lstPeople" ItemsSource="{Binding People}" ItemContainerStyle="{StaticResource ContainerStyle}" />

</Window>

回答1:

I'm pretty sure this is a bug, as several people have had this issue with the ListBoxItem template.

To fix, just add:

<Setter Property="Padding" Value="0,0,0,0"/>

to your ContainerStyle template. It's originally 2,0,0,0.



回答2:

Have a look at this answer to a similar question. Basically, if you want to restyle the ListBox so that selected items have a different background colour, it's easier to simply change the "HighlightBrush" colour so that the selected item uses the colour you want.



标签: wpf xaml listbox