C# 使用MouseOver/MouseEnter事件高亮显示包含控件的网格行

C# 使用MouseOver/MouseEnter事件高亮显示包含控件的网格行,c#,wpf,C#,Wpf,我试图找到一种好方法,当鼠标指针悬停在网格行上时,突出显示该行。我在每一行中都有一个矩形,我正在使用矩形.MouseEnter和矩形.MouseLeave事件上的触发器来更改其不透明度。问题是,当该行包含组合框控件、文本框控件等时,鼠标悬停在其上时会触发矩形.MouseLeave事件(示例中的第0行)。IsMouseOver事件也是如此。我可以通过将ishitestvisible设置为False(示例中的第1行)来避免这种情况,但这种设置会破坏目的(duh) 另一方面,我注意到Grid的IsMo

我试图找到一种好方法,当鼠标指针悬停在
网格
行上时,突出显示该行。我在每一行中都有一个
矩形
,我正在使用
矩形.MouseEnter
矩形.MouseLeave
事件上的触发器来更改其不透明度。问题是,当该行包含
组合框
控件、
文本框
控件等时,鼠标悬停在其上时会触发
矩形.MouseLeave
事件(示例中的第0行)。
IsMouseOver
事件也是如此。我可以通过将
ishitestvisible
设置为
False
(示例中的第1行)来避免这种情况,但这种设置会破坏目的(duh)

另一方面,我注意到
Grid
IsMouseOver
事件是
True
,即使鼠标悬停在
ishitestvisible
设置为
True
的控件上

那么,如何才能在
矩形
上获得
IsMouseOver
MouseEnter
/
MouseLeave
事件,而这些事件不会受到鼠标悬停在
网格
行中控件上的影响

或者就此而言,如果有人有更好的方法来实现这种行为,我洗耳恭听

这就是我试图实现的目标:

XAML:


以下是我解决您问题的方法:

  • 我同意@Mike的观点,马上设计StackPanel
  • 矩形不是必需的,而是确保将背景设置为透明,这样它将拾取鼠标悬停事件/属性更改
  • 代码如下所示:

    <Window.Resources>
        <Style TargetType="{x:Type StackPanel}"> <!-- this will apply to all StackPanels-->
            <Setter Property="Background" Value="Transparent"/> <!--without this, IsMouseOver is not triggered over the whitespace area-->
            <Setter Property="Orientation" Value="Horizontal"/>
            <Style.Triggers>
                <Trigger Property="IsMouseOver" Value="True">
                    <Setter Property="Background" Value="Red"/>
                </Trigger>
            </Style.Triggers>
        </Style>
    </Window.Resources>
    
    <Grid ShowGridLines="True">
        <Grid.RowDefinitions>
            <RowDefinition Height="*" />
            <RowDefinition Height="*" />
            <RowDefinition Height="*" />
            <RowDefinition Height="*" />
        </Grid.RowDefinitions>
    
        <!--Rectangle is removed-->
        <StackPanel Grid.Row="0"> <!-- StackPanel will use the style defined in the Window.Resources-->
            <ComboBox Height="30" Width="100" Margin="10,0,10,0" />
            <ComboBox Height="30" Width="100" Margin="0,0,10,0" />
            <ComboBox Height="30" Width="100" />
        </StackPanel>
    
        <StackPanel Grid.Row="1" >
            <ComboBox Height="30" Width="100" Margin="10,0,10,0" IsHitTestVisible="False" />
            <ComboBox Height="30" Width="100" Margin="0,0,10,0" IsHitTestVisible="False" />
            <ComboBox Height="30" Width="100" IsHitTestVisible="False" />
        </StackPanel>
    </Grid>
    

    仅用于文档,这是我最终获得动画的解决方案:

    <Window.Resources>
        <Style x:Key="StackPanelMouseOverHighlightStyle" TargetType="StackPanel">
            <Setter Property="Grid.Column" Value="0" />
            <Setter Property="Grid.ColumnSpan" Value="{Binding Path=ColumnDefinitions.Count,
                RelativeSource={RelativeSource Mode=FindAncestor, AncestorType=Grid}}" />
            <Setter Property="Background">
                <Setter.Value>
                    <SolidColorBrush x:Name="PanelBackgroundColor" Color="Red" Opacity="0" />
                </Setter.Value>
            </Setter>
            <Style.Triggers>
                <Trigger Property="IsMouseOver" Value="True">
                    <Trigger.EnterActions>
                        <BeginStoryboard>
                            <Storyboard>
                                <DoubleAnimation Storyboard.TargetProperty="(Panel.Background).(SolidColorBrush.Opacity)"
                                                    To="0.3" Duration="0:0:0.1" />
                            </Storyboard>
                        </BeginStoryboard>
                    </Trigger.EnterActions>
                    <Trigger.ExitActions>
                        <BeginStoryboard>
                            <Storyboard>
                                <DoubleAnimation Storyboard.TargetProperty="(Panel.Background).(SolidColorBrush.Opacity)"
                                                    To="0" Duration="0:0:0.1" />
                            </Storyboard>
                        </BeginStoryboard>
                    </Trigger.ExitActions>
                </Trigger>
            </Style.Triggers>
        </Style>
    </Window.Resources>
    
    <Grid ShowGridLines="True">
        <Grid.RowDefinitions>
            <RowDefinition Height="*" />
            <RowDefinition Height="*" />
            <RowDefinition Height="*" />
            <RowDefinition Height="*" />
        </Grid.RowDefinitions>
    
        <StackPanel Grid.Row="0" Orientation="Horizontal" Style="{StaticResource StackPanelMouseOverHighlightStyle}">
            <ComboBox Height="30" Width="100" Margin="10,0,10,0" />
            <ComboBox Height="30" Width="100" Margin="0,0,10,0" />
            <ComboBox Height="30" Width="100" />
        </StackPanel>
    
        <StackPanel Grid.Row="1" Orientation="Horizontal"  Style="{StaticResource StackPanelMouseOverHighlightStyle}">
            <ComboBox Height="30" Width="100" Margin="10,0,10,0" IsHitTestVisible="False" />
            <ComboBox Height="30" Width="100" Margin="0,0,10,0" IsHitTestVisible="False" />
            <ComboBox Height="30" Width="100" IsHitTestVisible="False" />
        </StackPanel>
    
    </Grid>
    
    
    
    为什么不将样式赋予您的
    stackpanel
    ?我并不总是使用
    stackpanel
    来包含控件。但是,这会改变有关事件的任何事情吗?我想答案就在我刚刚发布的副本中:-)嗯。。抢手货我找不到任何类似的病例。我来测试一下。
    <Window.Resources>
        <Style x:Key="StackPanelMouseOverHighlightStyle" TargetType="StackPanel">
            <Setter Property="Grid.Column" Value="0" />
            <Setter Property="Grid.ColumnSpan" Value="{Binding Path=ColumnDefinitions.Count,
                RelativeSource={RelativeSource Mode=FindAncestor, AncestorType=Grid}}" />
            <Setter Property="Background">
                <Setter.Value>
                    <SolidColorBrush x:Name="PanelBackgroundColor" Color="Red" Opacity="0" />
                </Setter.Value>
            </Setter>
            <Style.Triggers>
                <Trigger Property="IsMouseOver" Value="True">
                    <Trigger.EnterActions>
                        <BeginStoryboard>
                            <Storyboard>
                                <DoubleAnimation Storyboard.TargetProperty="(Panel.Background).(SolidColorBrush.Opacity)"
                                                    To="0.3" Duration="0:0:0.1" />
                            </Storyboard>
                        </BeginStoryboard>
                    </Trigger.EnterActions>
                    <Trigger.ExitActions>
                        <BeginStoryboard>
                            <Storyboard>
                                <DoubleAnimation Storyboard.TargetProperty="(Panel.Background).(SolidColorBrush.Opacity)"
                                                    To="0" Duration="0:0:0.1" />
                            </Storyboard>
                        </BeginStoryboard>
                    </Trigger.ExitActions>
                </Trigger>
            </Style.Triggers>
        </Style>
    </Window.Resources>
    
    <Grid ShowGridLines="True">
        <Grid.RowDefinitions>
            <RowDefinition Height="*" />
            <RowDefinition Height="*" />
            <RowDefinition Height="*" />
            <RowDefinition Height="*" />
        </Grid.RowDefinitions>
    
        <StackPanel Grid.Row="0" Orientation="Horizontal" Style="{StaticResource StackPanelMouseOverHighlightStyle}">
            <ComboBox Height="30" Width="100" Margin="10,0,10,0" />
            <ComboBox Height="30" Width="100" Margin="0,0,10,0" />
            <ComboBox Height="30" Width="100" />
        </StackPanel>
    
        <StackPanel Grid.Row="1" Orientation="Horizontal"  Style="{StaticResource StackPanelMouseOverHighlightStyle}">
            <ComboBox Height="30" Width="100" Margin="10,0,10,0" IsHitTestVisible="False" />
            <ComboBox Height="30" Width="100" Margin="0,0,10,0" IsHitTestVisible="False" />
            <ComboBox Height="30" Width="100" IsHitTestVisible="False" />
        </StackPanel>
    
    </Grid>