【问题标题】:How to remove the left-hand blue line on custom selected styled Listbox items?如何删除自定义选定样式列表框项目上的左侧蓝线?
【发布时间】:2009-05-08 13:52:37
【问题描述】:

我发现这段代码将默认选择样式替换为 ListBox 中选择 ListBoxItem 的自定义样式。但是,默认样式的左侧仍然有一条蓝色的小线,我无法通过任何填充或边距更改来删除它。

如何去掉那条蓝线并完全确定所选 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>

【问题讨论】:

    标签: wpf xaml listbox


    【解决方案1】:

    我很确定这是一个错误,因为很多人都遇到过 ListBoxItem 模板的问题。

    要修复,只需添加:

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

    到您的ContainerStyle 模板。原来是2,0,0,0

    【讨论】:

    • 是的,这行得通,你必须摆弄它才能恢复填充(我必须为每个子文本块应用边距,带填充的边框不断将蓝线带回来)但它是可以这样,谢谢。
    【解决方案2】:

    看看this answer to a similar question。基本上,如果您想重新设置 ListBox 的样式,使所选项目具有不同的背景颜色,只需更改“HighlightBrush”颜色,以便所选项目使用您想要的颜色。

    【讨论】:

      猜你喜欢
      • 2010-09-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-06-18
      • 1970-01-01
      • 1970-01-01
      • 2019-04-06
      • 1970-01-01
      相关资源
      最近更新 更多