这是我的第一个问题,所以我会尽力说清楚!
在 WinForms 工作了几年后,我才刚刚开始使用 WPF,而且对于我的一生,我无法触发按钮的事件。我目前在项目中拥有的只是一个无边框窗口,带有一个用于标题栏的自定义矩形。XAML 代码是:
<Window
x:Class="MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:M="clr-namespace:Metro_Test"
Title="MainWindow"
Height="720"
Width="1280"
IsTabStop="False"
AllowsTransparency="True"
Background="Transparent"
BorderBrush="#FF3F3F3F"
SnapsToDevicePixels="True"
TextOptions.TextFormattingMode="Display"
TextOptions.TextRenderingMode="ClearType"
WindowStyle="None"
WindowStartupLocation="CenterScreen" AllowDrop="True" ResizeMode="CanResizeWithGrip" PreviewMouseLeftButtonDown="HandleHeaderPreviewMouseDown">
<Window.Resources>
<Style x:Key="NoChromeButton" TargetType="{x:Type Button}">
<Setter Property="Background" Value="Transparent"/>
<Setter Property="BorderThickness" Value="1"/>
<Setter Property="Foreground" Value="{DynamicResource {x:Static SystemColors.ControlTextBrushKey}}"/>
<Setter Property="HorizontalContentAlignment" Value="Center"/>
<Setter Property="VerticalContentAlignment" Value="Center"/>
<Setter Property="Padding" Value="1"/>
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type Button}">
<Grid x:Name="Chrome" Background="{TemplateBinding Background}" SnapsToDevicePixels="true">
<ContentPresenter HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}" Margin="{TemplateBinding Padding}" RecognizesAccessKey="True" SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}" VerticalAlignment="{TemplateBinding VerticalContentAlignment}"/>
</Grid>
<ControlTemplate.Triggers>
<Trigger Property="IsEnabled" Value="false">
<Setter Property="Foreground" Value="#ADADAD"/>
<Setter Property="Opacity" TargetName="Chrome" Value="0.5"/>
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</Window.Resources>
<Border
x:Name="m_edgeBorder"
Margin="14"
Background="White">
<Border.Effect>
<DropShadowEffect
Opacity="0.999"
BlurRadius="14"
ShadowDepth="0"/>
</Border.Effect>
<Grid x:Name="MainGrid">
<Rectangle
Height="28"
Fill="Blue"
VerticalAlignment="Top"
AllowDrop="False"
MouseLeftButtonDown="HandleHeaderPreviewMouseDown"/>
<Button x:Name="CloseButton" Style="{DynamicResource NoChromeButton}" Click="HandleCloseClick" MouseEnter="HandleMouseEnter" MouseLeave="HandleMouseLeave" ClickMode="Release" HorizontalAlignment="Right" Margin="500,2,2,0" VerticalAlignment="Top" Width="24" Height="24">
<Image Source="Images\Gray\Close.png"></Image>
</Button>
<Button x:Name="MaximiseButton" Style="{DynamicResource NoChromeButton}" Click="HandleMaximiseClick" MouseEnter="HandleMouseEnter" MouseLeave="HandleMouseLeave" ClickMode="Release" HorizontalAlignment="Right" Margin="500,2,28,0" VerticalAlignment="Top" Width="24" Height="24">
<Image Source="Images\Gray\Add.png"></Image>
</Button>
<Button x:Name="MinimiseButton" Style="{DynamicResource NoChromeButton}" Click="HandleMinimiseClick" MouseEnter="HandleMouseEnter" MouseLeave="HandleMouseLeave" ClickMode="Release" HorizontalAlignment="Right" Margin="500,2,54,0" VerticalAlignment="Top" Width="24" Height="24">
<Image Source="Images\Gray\Minus.png"></Image>
</Button>
<Button x:Name="TestBtn" VerticalAlignment="Top" HorizontalAlignment="Right" Margin="500,2,80,0" Width="24" Height="24">
<Image Source="Images\Gray\Close.png"></Image>
</Button>
</Grid>
</Border>
</Window>
“代码隐藏”代码是:
Class MainWindow
Public Sub HandleHeaderPreviewMouseDown(sender As Object, e As MouseButtonEventArgs)
If e.LeftButton = MouseButtonState.Pressed Then
Me.DragMove()
End If
End Sub
Private Sub HandleCloseClick(sender As Object, e As MouseEventArgs)
Me.Close()
End Sub
Private Sub HandleMaximiseClick(sender As Object, e As MouseEventArgs)
If Me.WindowState = Windows.WindowState.Maximized Then
Me.WindowState = Windows.WindowState.Normal
ElseIf Me.WindowState = Windows.WindowState.Normal Then
Me.WindowState = Windows.WindowState.Maximized
End If
End Sub
Private Sub HandleMinimiseClick(sender As Object, e As MouseEventArgs)
Me.WindowState = Windows.WindowState.Minimized
End Sub
Private Sub HandleMouseLeave(sender As Button, e As MouseEventArgs)
End Sub
Private Sub HandleMouseEnter(sender As Button, e As MouseEventArgs)
End Sub
End Class
如您所见,我仍然没有在 MouseLeave 和 MouseEnter 事件中放置任何命令,因为出现了这个问题。
到目前为止,在这个项目中,我还没有一次能够让我的任何按钮工作。我尝试更改窗口、边框和网格的属性。我还尝试从 Rectangle 和 Window 中删除 PreviewMouseLeftButtonDown 事件,但无济于事。
非常感谢您对这个问题的任何帮助!