我創建了一個帶圓角的無邊框視窗,并為其添加了拖動事件和觸發器。這是簡單的代碼:
<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" 時也不會出現這種情況。
看起來它在單擊時丟失了滑鼠懸停觸發器,但我想不出任何避免閃爍出現的好方法,并滿足圓角、可拖動和觸發器的需求。
uj5u.com熱心網友回復:
好吧,雖然我不知道為什么只有圓角會導致 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;
}
似乎作業正常。
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/492188.html
