【问题标题】:DragMove() will make the Border with cornerRadius lose its mouseover trigger state?DragMove() 会使有cornerRadius 的Border 失去鼠标悬停触发状态?
【发布时间】:2022-06-14 19:02:19
【问题描述】:

我创建了一个带圆角的无边框窗口,并为其添加了拖动事件和触发器。这是简单的代码:

<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();
        }

但是当我运行exe文件时,点击窗口内的空白区域,按钮会出现很明显的闪烁情况。

奇怪的是,在 Visual Studio 中调试而不是双击文件时几乎不会出现这种情况,而 CornerRadius="0" 时也不会出现这种情况。

看起来它在点击时失去了鼠标悬停触发器,但我想不出任何好的方法来避免出现闪烁,并满足对圆角的需求可拖动,并带有触发器

【问题讨论】:

    标签: wpf triggers border drag datatrigger


    【解决方案1】:

    好吧,虽然我不知道为什么只有圆角会导致 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;
            }
    

    似乎工作正常。

    【讨论】:

      猜你喜欢
      • 2012-11-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-08-21
      相关资源
      最近更新 更多