更改ItemContainerStyle时ListBoxItem选择的问题

时间:2015-01-21 12:45:07

标签: wpf xaml selecteditem listboxitem listbox-control

我更改了ItemContainerStyle的样式,结果是:

enter image description here

我很喜欢这个结果,但是在测试可用性时,我注意到当我选择没有文字或图像的区域(图像上的红色区域)时,“IsSelected”不会触发并且不会选择该项目。没有风格改变,那个问题就不会发生..

有人知道解决这个问题的原因吗?

这是我的xaml代码:

<DockPanel>
    <ListBox x:Name="lvCustomers" Margin="0" BorderThickness="0" SelectionMode="Single" 
                VerticalAlignment="Stretch" HorizontalContentAlignment="Stretch"
                ScrollViewer.VerticalScrollBarVisibility="Auto" Background="{x:Null}" >
        <ListBox.ItemContainerStyle>
            <Style TargetType="{x:Type ListBoxItem}">
                <Setter Property="SnapsToDevicePixels" Value="True"/>
                <Setter Property="OverridesDefaultStyle" Value="True"/>
                <Setter Property="Template">
                    <Setter.Value>
                        <ControlTemplate TargetType="{x:Type ListBoxItem}">
                            <Grid>
                                <Border Name="BackgroundBorder" SnapsToDevicePixels="True"/>
                                <Border Name="Border">
                                    <ContentPresenter />
                                </Border>
                            </Grid>
                            <ControlTemplate.Triggers>
                                <Trigger Property="IsSelected" Value="True">
                                    <Setter TargetName="BackgroundBorder" Property="Background" Value="Black" />
                                    <Setter TargetName="BackgroundBorder" Property="Opacity" Value="0.5" />
                                </Trigger>
                                <MultiTrigger>
                                    <MultiTrigger.Conditions>
                                        <Condition Property="IsSelected" Value="True"/>
                                        <Condition Property="IsFocused" Value="False"/>
                                    </MultiTrigger.Conditions>
                                    <Setter TargetName="BackgroundBorder" Property="Background" Value="Black" />
                                </MultiTrigger>
                            </ControlTemplate.Triggers>
                        </ControlTemplate>
                    </Setter.Value>
                </Setter>
            </Style>
        </ListBox.ItemContainerStyle>

        <ListBox.ItemTemplate>
            <DataTemplate>
                <DockPanel Margin="10,0,0,5" HorizontalAlignment="Stretch">
                    <DockPanel Margin="0,10,10,10" DockPanel.Dock="Left" HorizontalAlignment="Stretch">
                        <TextBlock FontWeight="Bold" Foreground="White" x:Name="customerDetailsName" Text="{Binding Name}" FontSize="16"  DockPanel.Dock="Top"/>
                        <DockPanel DockPanel.Dock="Top">
                            <TextBlock Foreground="White" x:Name="customerDetailsCityContent" Text="{Binding City}" FontSize="14" />
                            <TextBlock Foreground="White" x:Name="customerDetailsBarraContent" Text="-" FontSize="12" />
                            <TextBlock Foreground="White" x:Name="customerDetailsStateContent" Text="{Binding Region}" FontSize="14" />
                        </DockPanel>
                        <TextBlock Foreground="White" x:Name="customerDetailsCNPJContent" Text="{Binding CNPJ}" FontSize="14" />
                    </DockPanel>
                    <Image x:Name="customerImageStatus" Source="{Binding StatusImage}" Width="22" Height="22" Margin="10,0,25,0" DockPanel.Dock="Right" HorizontalAlignment="Right" />
                </DockPanel>
            </DataTemplate>
        </ListBox.ItemTemplate>
    </ListBox>

</DockPanel>

提前致谢。

1 个答案:

答案 0 :(得分:1)

请尝试设置背景=&#34;透明&#34;或IsHitTestVisible =&#34; True&#34;在控制模板中使用网格和边框。为了实现透明对象的命中测试,您应该将背景设置为透明。