如何避免在ListBox中选择更改datatemplate?

时间:2013-02-28 17:31:26

标签: c# wpf templates listbox selecteditem

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

列表框有一个datatemplate,但我有一个问题,对于列表框中的最后一个选定项目,它会将背景颜色更改为白色?我怎么能避免这个?

我上传了一张图片以供说明。

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

问候。

1 个答案:

答案 0 :(得分: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>