简体   繁体   English

DragMove() 会使有cornerRadius 的Border 失去鼠标悬停触发状态?

[英]DragMove() will make the Border with cornerRadius lose its mouseover trigger state?

I have created a borderless window with rounded corners, and added the drag event and a trigger to it.我创建了一个带圆角的无边框窗口,并为其添加了拖动事件和触发器。 Here is the simple code:这是简单的代码:

<Window x:Class="DebugTest.MainWindow"
        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:DebugTest"
        mc:Ignorable="d" Height="200" Width="200"
        AllowsTransparency="True" WindowStyle="None" Background="Transparent">
    <Border x:Name="MainBorder" CornerRadius="15" Background="White" BorderBrush="Black" BorderThickness="1">
        <Grid>
            <Grid.Style>
                <Style TargetType="Grid">
                    <Setter Property="Visibility" Value="Hidden" />
                    <Style.Triggers>
                        <DataTrigger Binding="{Binding ElementName=MainBorder,Path=IsMouseOver}" Value="True">
                            <Setter Property="Visibility" Value="Visible"/>
                        </DataTrigger>
                    </Style.Triggers>
                </Style>
            </Grid.Style>
            <Button Content="x" HorizontalAlignment="Right" VerticalAlignment="Top" 
                    Margin="5" Height="20" Width="20" Click="Button_Click"/>
        </Grid>
    </Border>
</Window>
        public MainWindow()
        {
            InitializeComponent();
        }
        protected override void OnMouseLeftButtonDown(MouseButtonEventArgs e)
        {
            base.OnMouseLeftButtonDown(e);
            this.DragMove();
        }
        private void Button_Click(object sender, RoutedEventArgs e)
        {
            this.Close();
        }

But when I run the exe file, click on the blank area within the window, the button will appear very obvious flickering situation.但是当我运行exe文件时,点击窗口内的空白区域,按钮会出现很明显的闪烁情况。

Strangely enough, this situation hardly occurs when debugging in Visual Studio instead of double click the file, and it also doesn't happen while CornerRadius="0".奇怪的是,这种情况在 Visual Studio 中调试而不是双击文件时几乎不会发生,在 CornerRadius="0" 时也不会发生。

It looks like it lost the mouseover trigger on click, but I can't think of any good way to avoid flicker appearing, and to satisfy the need for both with rounded corners , draggable , and with trigger .看起来它在单击时丢失了鼠标悬停触发器,但我想不出任何避免闪烁出现的好方法,并满足圆角可拖动触发器的需求。

Well, although I don't know why only rounded corners would cause DragMove() to trigger the MouseLeave event, I circumvented this with background code instead of using xaml trigger.好吧,虽然我不知道为什么只有圆角会导致 DragMove() 触发 MouseLeave 事件,但我使用后台代码而不是使用 xaml 触发器来绕过它。

    <Border x:Name="MainBorder" CornerRadius="15" Background="White" 
            BorderBrush="Black" BorderThickness="1"
            MouseEnter="MainBorder_MouseEnter" MouseLeave="MainBorder_MouseLeave">
        <Grid Visibility="Hidden" x:Name="TriggerBorder">
            <Button Content="x" HorizontalAlignment="Right" VerticalAlignment="Top" 
                    Margin="5" Height="20" Width="20" Click="Button_Click"/>
        </Grid>
    </Border>
        bool dragMoving = false;
        public MainWindow()
        {
            InitializeComponent();
        }        
        private void Button_Click(object sender, RoutedEventArgs e)
        {
            this.Close();
        }       
        protected override void OnMouseLeftButtonDown(MouseButtonEventArgs e)
        {
            base.OnMouseLeftButtonDown(e);
            dragMoving = true;
            this.DragMove();
            dragMoving = false;
        }
        private void MainBorder_MouseEnter(object sender, MouseEventArgs e)
        {
            TriggerBorder.Visibility = Visibility.Visible;
        }

        private void MainBorder_MouseLeave(object sender, MouseEventArgs e)
        {
            if (dragMoving) return;
            TriggerBorder.Visibility = Visibility.Hidden;
        }

Seems to work fine.似乎工作正常。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM