You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

WPF问题:鼠标移至父窗体时IsMouseOver仍保持True

WPF自定义标题栏关闭按钮IsMouseOver异常问题

我使用WindowChrome自定义窗口标题栏,移除窗口图标仅保留关闭按钮,相关代码如下:

XAML代码

<Window x:Class="TestProject.testWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:RadScene"
        mc:Ignorable="d"
        Title="" Height="420" Width="370" BorderBrush="#006cc1" BorderThickness="0.7" WindowStyle="None">
    <WindowChrome.WindowChrome>
        <WindowChrome CaptionHeight="30" />
    </WindowChrome.WindowChrome>

    <Window.Resources>
        <ResourceDictionary>
            <Style x:Key="TitleBarButtonStyle" TargetType="{x:Type Button}">
                <Setter Property="Foreground" Value="Black" />
                <Setter Property="Padding" Value="0" />
                <Setter Property="WindowChrome.IsHitTestVisibleInChrome" Value="True" />
                <Setter Property="IsTabStop" Value="False" />
                <Setter Property="Template">
                    <Setter.Value>
                        <ControlTemplate TargetType="{x:Type Button}">
                            <Grid x:Name="border" Background="Transparent" SnapsToDevicePixels="true">
                                <ContentPresenter x:Name="contentPresenter" Margin="0" HorizontalAlignment="Center" VerticalAlignment="Center" Focusable="False" RecognizesAccessKey="True" />
                            </Grid>
                            <ControlTemplate.Triggers>
                                <Trigger Property="IsMouseOver" Value="true">
                                    <Setter TargetName="border" Property="Background" Value="Red" />
                                </Trigger>
                            </ControlTemplate.Triggers>
                        </ControlTemplate>
                    </Setter.Value>
                </Setter>
            </Style>
            <Style x:Key="CloseButtonStyle" TargetType="Button" BasedOn="{StaticResource TitleBarButtonStyle}"/>
        </ResourceDictionary>
    </Window.Resources>

    
    <Grid Grid.Row="0">
        <Grid.RowDefinitions>
            <RowDefinition Height="30"/>
            <RowDefinition Height="*"/>
            <RowDefinition Height="Auto"/>
        </Grid.RowDefinitions>

        <Grid Grid.Row="0" Height="30" Background="white">
            <StackPanel Orientation="Horizontal" HorizontalAlignment="Left" VerticalAlignment="Center">
                <TextBlock Text="Caption" FontSize="16" Foreground="#006cc1" Margin="5 3 0 0" />
            </StackPanel>

            <!--Caption buttons-->
            <StackPanel Orientation="Horizontal" HorizontalAlignment="Right" >
                <Button x:Name="buttonClose" Style="{StaticResource CloseButtonStyle}">
                    <Path Width="46" Height="32" Data="M 18,11 27,20 M 18,20 27,11" Stroke="{Binding Path=Foreground, RelativeSource={RelativeSource AncestorType={x:Type Button}}}" StrokeThickness="0.6" />
                </Button>
            </StackPanel>
        </Grid>

    <StackPanel Grid.Row="1" Margin="10" Orientation="Vertical">
        <TextBlock Text="Content" Margin="0,5,0,0" FontSize="15" Foreground="#646464">Time</TextBlock>            
    </StackPanel>

    </Grid>
</Window>

C#代码

testWindow tw = new testWindow();
tw.Owner = this;
tw.WindowStartupLocation = WindowStartupLocation.CenterOwner;
tw.ShowDialog();

所有功能运行正常,但存在异常:当鼠标从关闭按钮向右移动到父窗体时,IsMouseOver事件未转为False,其他方向移动均正常;若将子窗口移出父窗体范围,则无此问题。


问题解决更新

经排查,问题根源在于使用ShowDialog(),改用Show()即可解决该异常问题。


内容的提问来源于stack exchange,提问作者T James

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.15 18:15:05