在取消选择项目后,IsMouseOver不起作用

时间:2013-03-27 12:15:43

标签: wpf

我有一个问题,我取消选中后,我的列表视图中的取消选择的项目不会显示“IsMouseOver”的颜色,除非我选择其他内容然后选择它。在我的ItemContainerStyle中,我将focus / unfocus的默认颜色设置为透明,因为我想定义自己的值。任何人都可以看看这个并告诉我为什么“IsMouseOver”对取消选择的项目不起作用吗?

XAML:

<ListView Name="lstPhoneCarrier" HorizontalAlignment="Left" VerticalAlignment="Bottom" Width="408" Height="145" 
  ItemsSource="{Binding ShowAllCarrierInfo, Mode=TwoWay}" 
  ScrollViewer.HorizontalScrollBarVisibility="Hidden" ScrollViewer.VerticalScrollBarVisibility="Visible" 
  SelectionMode="Single">

<ListView.ItemContainerStyle>
    <Style TargetType="{x:Type ListViewItem}">
        <Style.Resources>
            <SolidColorBrush x:Key="{x:Static SystemColors.HighlightBrushKey}" Color="Transparent" />
            <SolidColorBrush x:Key="{x:Static SystemColors.ControlBrushKey}" Color="Transparent" />
        </Style.Resources>
        <Style.Triggers>
            <Trigger Property="IsMouseOver" Value="True">
                <Setter Property="Background" Value="Yellow" />
            </Trigger>
        </Style.Triggers>
    </Style>
</ListView.ItemContainerStyle>

<ListView.ItemTemplate>
    <DataTemplate>
        <Label x:Name="lblCarrier" HorizontalAlignment="Left">
            <Label.Content>
                <StackPanel Orientation="Horizontal">
                    <TextBlock>
                        <TextBlock.Text>
                            <MultiBinding StringFormat="{}{0}-{1}">
                                <Binding Path="Name" />
                                <Binding Path="Description" />
                             </MultiBinding>
                        </TextBlock.Text>
                    </TextBlock>
                </StackPanel>
            </Label.Content>
        </Label>
        <DataTemplate.Triggers>
            <DataTrigger Binding="{Binding Path=CarrierSelected}" Value="True" >
                <Setter TargetName="lblCarrier" Property="Background" Value="Pink" />
            </DataTrigger>
        </DataTemplate.Triggers>
    </DataTemplate>
</ListView.ItemTemplate>

1 个答案:

答案 0 :(得分:0)

您已覆盖突出显示画笔,以便用户无法看到选择,但内部selection仍在使用captures the mouse,因此MouseOver events not getting raised for that item

您需要为Focusablefalse设置为listBoxItem,以便鼠标无法在ItemContainerStyle中捕获 -

<ListView.ItemContainerStyle>
    <Style TargetType="{x:Type ListViewItem}">
         <Setter Property="Focusable" Value="False"/> // <---- HERE
        <Style.Resources>
            <SolidColorBrush x:Key="{x:Static SystemColors.HighlightBrushKey}" 
                             Color="Transparent" />
            <SolidColorBrush x:Key="{x:Static SystemColors.ControlBrushKey}"
                             Color="Transparent" />
        </Style.Resources>
        <Style.Triggers>
            <Trigger Property="IsMouseOver" Value="True">
                <Setter Property="Background" Value="Yellow" />
            </Trigger>
        </Style.Triggers>
    </Style>
</ListView.ItemContainerStyle>