通过带有任何背景画笔的DataTemplate呈现的项目在项目后面呈现选择着色

时间:2010-04-13 19:51:12

标签: wpf xaml binding

我有一个ListBox,它使用DataTemplate来渲染数据绑定项。 datatemplate的XAML如下:

<DataTemplate x:Key="NameResultTemplate">
                <WrapPanel x:Name="PersonResultWrapper" Margin="0" Orientation="Vertical" Background="{Binding Converter={StaticResource NameResultToColor}, Mode=OneWay}" >
                    <i:Interaction.Triggers>
                        <i:EventTrigger EventName="MouseDown">
                            <cmd:EventToCommand x:Name="SelectPersonEventCommand" Command="{Binding Search.SelectedPersonCommand, Mode=OneWay, Source={StaticResource Locator}}" CommandParameter="{Binding Mode=OneWay}" />
                        </i:EventTrigger>
                    </i:Interaction.Triggers>
                    <TextBlock x:Name="txtPersonName" TextWrapping="Wrap" Margin="0" VerticalAlignment="Top" Text="{Binding PersonName}" FontSize="24" Foreground="Black" />
                    <TextBlock x:Name="txtAgencyName" TextWrapping="Wrap" Text="{Binding AgencyName}" HorizontalAlignment="Left" VerticalAlignment="Bottom" Margin="0" FontStyle="Italic" Foreground="Black" />
                    <TextBlock x:Name="txtPIDORI" TextWrapping="Wrap" Text="{Binding PIDORI}" HorizontalAlignment="Left" VerticalAlignment="Bottom" Margin="0" FontStyle="Italic" Foreground="Black" />
                    <TextBlock x:Name="txtDescriptors" TextWrapping="Wrap" Text="{Binding DisplayDescriptors}" Margin="0" VerticalAlignment="Top" Foreground="Black"/>
                    <Separator Margin="0" Width="400" />
                </WrapPanel>
            </DataTemplate>

请注意,有一个名为NameResultToColor的值转换器,它根据特定情况将渲染的WrapPanel的背景画笔更改为渐变画笔。

所有这些都可以按照我的预期工作,除非您单击任何渲染的ListBox项目。单击一个时,只有最轻微的选择颜色标志(默认蓝色)。我可以在渐变拉丝项目下面看到它的一点点。如果我将背景画笔重置为“无画笔”,则选择渲染正常。如果我将背景画笔设置为纯色,它也无法按照我的预期渲染。

如何让选择颜色显示在最上面?什么是选择渲染?

1 个答案:

答案 0 :(得分:3)

问题是您的项目模板正在由ListBoxItem绘制的选区上绘制。如果您想确保保留颜色,可以添加DataTrigger以在选择项目时将WrapPanel的背景设置为null:

<DataTemplate x:Key="NameResultTemplate">
    <WrapPanel x:Name="PersonResultWrapper">
        ...
    </WrapPanel>
    <DataTemplate.Triggers>
        <DataTrigger Binding="{Binding IsSelected, RelativeSource={RelativeSource AncestorType={x:Type ListBoxItem}}}" Value="True">
            <Setter TargetName="PersonResultWrapper" Property="Background" Value="{x:Null}" />
        </DataTrigger>
    </DataTemplate.Triggers>
</DataTemplate>