如何删除自定义选定样式的列表框项目上的左侧蓝线?

时间:2009-05-08 02:31:10

标签: wpf xaml listbox

我发现这个代码用ListBox中的select ListBoxItem的自定义样式替换了默认的select样式。但是,默认样式左侧仍然有一点蓝线,我无法删除任何填充或边距更改。

如何删除该蓝线并完全确定所选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>

2 个答案:

答案 0 :(得分:4)

我很确定这是一个错误,因为有些人对ListBoxItem模板有这个问题。

要修复,只需添加:

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

到您的ContainerStyle模板。它最初是2,0,0,0

答案 1 :(得分:1)

看看this answer to a similar question。基本上,如果您想重新放置ListBox以使所选项具有不同的背景颜色,则更容易简单地更改“HighlightBrush”颜色,以便所选项目使用您想要的颜色。