0

我想将 MouseEnter 和 MouseLeave EventTriggers 应用于 WPF GridView 的整行。我使用下面的代码将事件触发器添加到单个列中,当鼠标移到该列中的文本上时触发。

<ListView x:Name="MyList" ItemsSource="{Binding listItems}">
        <ListView.View>
            <GridView>
                <GridViewColumn Header="Tag" Width="100" >
                    <GridViewColumn.CellTemplate>
                        <DataTemplate>
                            <TextBlock Text="{Binding Tag}" Foreground="{Binding ForegroundColor}">
                                <i:Interaction.Triggers>
                                    <i:EventTrigger EventName="MouseEnter">
                                        <i:InvokeCommandAction Command="{Binding Path=MouseEnterCommand}"/>
                                    </i:EventTrigger>
                                    <i:EventTrigger EventName="MouseLeave">
                                        <i:InvokeCommandAction Command="{Binding Path=MouseLeaveCommand}"/>
                                    </i:EventTrigger>
                                </i:Interaction.Triggers>
                            </TextBlock>
                        </DataTemplate>
                    </GridViewColumn.CellTemplate>
                </GridViewColumn>
                <GridViewColumn Header="Description" Width="300" DisplayMemberBinding="{Binding Description}"/>
                <GridViewColumn Header="Qty" Width="80" DisplayMemberBinding="{Binding Qty}"/>
            </GridView>
        </ListView.View>
    </ListView>

与其在每个 CellTemplate 中复制相同的 Trigger 代码,是否有更简洁的方法将触发器应用于整个行,以便当鼠标进入或离开该行上的任何区域时,触发事件?

4

1 回答 1

0

如果要将 MouseEnter 和 MouseLeave EventTriggers 应用于 listview 的整行,则可以重写 ListviewItem 的样式,如下所示:

   <Style x:Key="ListViewItemFocusVisual">
        <Setter Property="Control.Template">
            <Setter.Value>
                <ControlTemplate>
                    <Rectangle RadiusY="2" RadiusX="2" Stroke="#8E6EA6F5" StrokeThickness="1"/>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>
    <LinearGradientBrush x:Key="ListItemHoverFill" EndPoint="0,1" StartPoint="0,0">
        <GradientStop Color="#FFF1FBFF" Offset="0"/>
        <GradientStop Color="#FFD5F1FE" Offset="1"/>
    </LinearGradientBrush>
    <LinearGradientBrush x:Key="ListItemSelectedFill" EndPoint="0,1" StartPoint="0,0">
        <GradientStop Color="#FFD9F4FF" Offset="0"/>
        <GradientStop Color="#FF9BDDFB" Offset="1"/>
    </LinearGradientBrush>
    <LinearGradientBrush x:Key="ListItemSelectedInactiveFill" EndPoint="0,1" StartPoint="0,0">
        <GradientStop Color="#FFEEEDED" Offset="0"/>
        <GradientStop Color="#FFDDDDDD" Offset="1"/>
    </LinearGradientBrush>
    <LinearGradientBrush x:Key="ListItemSelectedHoverFill" EndPoint="0,1" StartPoint="0,0">
        <GradientStop Color="#FFEAF9FF" Offset="0"/>
        <GradientStop Color="#FFC9EDFD" Offset="1"/>
    </LinearGradientBrush> 
    <Style x:Key="ListViewItemStyle1" TargetType="{x:Type ListViewItem}">
        <Setter Property="FocusVisualStyle" Value="{StaticResource ListViewItemFocusVisual}"/>
        <Setter Property="Background" Value="Transparent"/>
        <Setter Property="BorderBrush" Value="Transparent"/>
        <Setter Property="BorderThickness" Value="1"/>
        <Setter Property="Margin" Value="0,0,0,1"/>
        <Setter Property="Padding" Value="5,2,5,2"/>
        <Setter Property="VerticalContentAlignment" Value="Center"/>
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="{x:Type ListViewItem}">
                    <Border BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}" Background="{TemplateBinding Background}" CornerRadius="2" SnapsToDevicePixels="true">
                        <Border x:Name="InnerBorder" BorderThickness="1" CornerRadius="1">
                            <Grid>
                                <Grid.RowDefinitions>
                                    <RowDefinition MaxHeight="11"/>
                                    <RowDefinition/>
                                </Grid.RowDefinitions>
                                <Rectangle x:Name="UpperHighlight" Fill="#75FFFFFF" Visibility="Collapsed"/>
                                <GridViewRowPresenter Grid.RowSpan="2" SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}" VerticalAlignment="{TemplateBinding VerticalContentAlignment}"/>
                            </Grid>
                        </Border>
                    </Border>
                    <ControlTemplate.Triggers>
                        <Trigger Property="IsMouseOver" Value="true">
                            <Setter Property="Background" Value="{StaticResource ListItemHoverFill}"/>
                            <Setter Property="BorderBrush" Value="#FFCCF0FF"/>
                            <Setter Property="Visibility" TargetName="UpperHighlight" Value="Visible"/>
                        </Trigger>
                        <Trigger Property="IsSelected" Value="true">
                            <Setter Property="Background" Value="{StaticResource ListItemSelectedFill}"/>
                            <Setter Property="BorderBrush" Value="#FF98DDFB"/>
                            <Setter Property="BorderBrush" TargetName="InnerBorder" Value="#80FFFFFF"/>
                            <Setter Property="Visibility" TargetName="UpperHighlight" Value="Visible"/>
                            <Setter Property="Fill" TargetName="UpperHighlight" Value="#40FFFFFF"/>
                        </Trigger>
                        <MultiTrigger>
                            <MultiTrigger.Conditions>
                                <Condition Property="IsSelected" Value="true"/>
                                <Condition Property="Selector.IsSelectionActive" Value="false"/>
                            </MultiTrigger.Conditions>
                            <Setter Property="Background" Value="{StaticResource ListItemSelectedInactiveFill}"/>
                            <Setter Property="BorderBrush" Value="#FFCFCFCF"/>
                        </MultiTrigger>
                        <MultiTrigger>
                            <MultiTrigger.Conditions>
                                <Condition Property="IsSelected" Value="true"/>
                                <Condition Property="IsMouseOver" Value="true"/>
                            </MultiTrigger.Conditions>
                            <Setter Property="Background" Value="{StaticResource ListItemSelectedHoverFill}"/>
                            <Setter Property="BorderBrush" Value="#FF98DDFB"/>
                        </MultiTrigger>
                        <Trigger Property="IsEnabled" Value="false">
                            <Setter Property="Foreground" Value="{DynamicResource {x:Static SystemColors.GrayTextBrushKey}}"/>
                        </Trigger>
                    </ControlTemplate.Triggers>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>
于 2018-11-07T08:08:18.227 回答