Stackpanel IsMouseOver 触发器不会更改为 true

Stackpanel IsMouseOver Trigger does not change to true

我有一个 Stackpanel,里面有一张图片。图像是部分透明的。 所以我希望背景是红色的,当鼠标没有结束时(这有效)。但是当鼠标悬停时它应该变成绿色。但它不起作用。你能帮帮我吗

<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
                    xmlns:shell="clr-namespace:System.Windows.Shell;assembly=PresentationFramework">
    <Style x:Key="PhoenixWindowStyle" TargetType="{x:Type Window}">
        <Setter Property="shell:WindowChrome.WindowChrome">
            <Setter.Value>
                <shell:WindowChrome GlassFrameThickness="0"
                            ResizeBorderThickness="1"
                            CaptionHeight="32"
                            CornerRadius="0"/>
            </Setter.Value>
        </Setter>
        <Setter Property="Background" Value="{DynamicResource DefaultBackgroundBrush}"/>
        <Setter Property="MinWidth" Value="100"/>
        <Setter Property="MinHeight" Value="100"/>
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="{x:Type Window}">
                    <Grid Background="{DynamicResource BorderBrush}">
                        <Grid.ColumnDefinitions>
                            <ColumnDefinition Width="1"/>
                            <ColumnDefinition Width="*"/>
                            <ColumnDefinition Width="1"/>
                        </Grid.ColumnDefinitions>
                        <Grid.RowDefinitions>
                            <RowDefinition Height="32"/>
                            <RowDefinition Height="*"/>
                            <RowDefinition Height="1"/>
                        </Grid.RowDefinitions>

                        <DockPanel Grid.Column="1" Grid.Row="0">
                            <TextBlock DockPanel.Dock="Left" Margin="0, 2, 0, 2" Padding="0">
                                <Image Width="24" 
                                       Height="24" 
                                       Margin="2"
                                       Source="{TemplateBinding Icon}"
                                       SnapsToDevicePixels="True"
                                       RenderOptions.EdgeMode="Aliased" />
                                <Run BaselineAlignment="Center" 
                                     Text="{TemplateBinding Title}"
                                     Foreground="{DynamicResource DefaultBackgroundBrush}"/>
                            </TextBlock>
                            <TextBlock DockPanel.Dock="Right" HorizontalAlignment="Right">
                            <!--This it the part I showed before -->
                                <StackPanel Width="38" Height="32">
                                    <StackPanel.Style>
                                        <Style TargetType="StackPanel">
                                            <Style.Triggers>
                                                <Trigger Property="IsMouseOver" Value="True">
                                                    <Setter Property="Background" Value="Green"/>
                                                </Trigger>
                                                <Trigger Property="IsMouseOver" Value="False">
                                                    <Setter Property="Background" Value="Red"/>
                                                </Trigger>
                                            </Style.Triggers>
                                        </Style>
                                    </StackPanel.Style>
                                    <Image 
                                        Width="38" 
                                        Height="32" 
                                        Margin="0"
                                        Source="../Images/FrameControlIcons/38x32/close.png"/>
                                </StackPanel>
                                <!--/////////////////////-->
                            </TextBlock>
                        </DockPanel>

                        <DockPanel Grid.Column="1" Grid.Row="1" >
                            <ContentPresenter />
                        </DockPanel>
                    </Grid>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>
</ResourceDictionary>

我的同事发现了这个问题。问题是,我的图像被 WindowChromeCaptionHeight 覆盖了。当我将 CaptionHeigt 设置为零时,它起作用了。

所以我找到了使两者都起作用的解决方案。 CaptionHeight(拖动 window)和元素上的鼠标事件被 CaptionHeight.

覆盖

我必须在受影响的元素上设置此 属性:

shell:WindowChrome.IsHitTestVisibleInChrome="True"

我在这里找到了这个解决方案:

How can I add a button to the WPF caption bar while using custom window chrome?