【问题标题】:Issues with ListBoxItem selection when ItemContainerStyle was changed更改 ItemContainerStyle 时 ListBoxItem 选择的问题
【发布时间】:2015-01-21 12:45:07
【问题描述】:

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

我很喜欢这个结果,但是在测试可用性时,我注意到当我选择一个没有文本或图像的区域(图像上的红色区域)时,“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>

提前致谢。

【问题讨论】:

  • 是的!这是正确的!非常感谢。

标签: wpf xaml selecteditem listboxitem listbox-control


【解决方案1】:

请尝试在controltemplate中将Background="Transparent"或IsHitTestVisible="True"设置为grid和border。要实现透明物体的命中测试,您应该将背景设置为透明。

【讨论】:

    猜你喜欢
    • 2010-09-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-01-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多