0

我在 WPF 应用程序中使用 VS2012。我在主窗口中有一个列表框。列表框的 background = null - 这意味着背景等于主窗口背景。

列表框有一个数据模板,但我有一个问题,对于列表框中的最后一个选定项目,它会将背景颜色更改为白色?我怎样才能避免这种情况?

我上传了一张图片作为说明。

*编辑-这张图片说明了问题 http://i57.photobucket.com/albums/g202/RolleKn/pic2_zps94f2907a.jpg

问候。

4

1 回答 1

0

要删除 SelectedItem 的背景更改,您必须覆盖 listboxitem 的控件模板。

试试这个

<Style  TargetType="{x:Type ListBoxItem}">
<Setter Property="Background" Value="Transparent"/>
<Setter Property="HorizontalContentAlignment" Value="{Binding HorizontalContentAlignment, RelativeSource={RelativeSource AncestorType={x:Type ItemsControl}}}"/>
<Setter Property="VerticalContentAlignment" Value="{Binding VerticalContentAlignment, RelativeSource={RelativeSource AncestorType={x:Type ItemsControl}}}"/>
<Setter Property="Padding" Value="2,0,0,0"/>
<Setter Property="Template">
    <Setter.Value>
        <ControlTemplate TargetType="{x:Type ListBoxItem}">
            <Border x:Name="Bd" BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}" Background="{TemplateBinding Background}" Padding="{TemplateBinding Padding}" SnapsToDevicePixels="true">
                <ContentPresenter HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}" SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}" VerticalAlignment="{TemplateBinding VerticalContentAlignment}"/>
            </Border>
            <ControlTemplate.Triggers>

                <Trigger Property="IsEnabled" Value="false">
                    <Setter Property="Foreground" Value="{DynamicResource {x:Static SystemColors.GrayTextBrushKey}}"/>
                </Trigger>
            </ControlTemplate.Triggers>
        </ControlTemplate>
    </Setter.Value>
</Setter>
</Style>
于 2013-02-28T17:36:25.750 回答