Stackpanel IsMouseOver触发器不会更改为true

时间:2015-05-21 11:07:27

标签: wpf xaml triggers

我有一个带有图像的Stackpanel。图像部分是transperent。 所以我希望背景为红色,当鼠标没有结束时(这是有效的)。但是当鼠标结束时它应该变成绿色。但它不起作用。你能帮帮我吗?

<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>

1 个答案:

答案 0 :(得分:1)

我的同事认识到了这个问题。问题是,我的图片被CaptionHeight的{​​{1}}覆盖了。当我将WindowChrome设置为零时,它可以工作。

所以我找到了一个让两者兼顾的解决方案。 CaptionHeigt上的CaptionHeight(拖动窗口)和元素上CaptionHeight覆盖的鼠标事件。

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

shell:WindowChrome.IsHitTestVisibleInChrome="True"

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

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