表单按钮不起作用
这是我的第一个问题,所以我会尽力说清楚!
在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="ImagesGrayClose.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="ImagesGrayAdd.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="ImagesGrayMinus.png"></Image>
</Button>
<Button x:Name="TestBtn" VerticalAlignment="Top" HorizontalAlignment="Right" Margin="500,2,80,0" Width="24" Height="24">
<Image Source="ImagesGrayClose.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事件中放置任何命令,因为这个问题已经出现。
到目前为止,在这个项目中,我还没有能够让我的任何按钮工作。 我试图改变窗口,边框和网格的属性。 我也尝试从矩形和窗口中移除PreviewMouseLeftButtonDown事件无济于事。
任何帮助这个问题,非常感谢!
那么你应该考虑Steve发布的所有内容,并且如果你不知道确切的签名,尽量不要手动创建事件处理程序。 但更大的问题在于,您为整个窗口处理鼠标预览事件( PreviewMouseLeftButtonDown="HandleHeaderPreviewMouseDown"
),并在事件处理函数中调用Me.DragMove()
- 将事件标记为已处理,因此不再有鼠标事件处理程序要求儿童控制。 首先阅读有关路由事件的信息。 你应该调用Me.DragMove()
,只有当鼠标被按下并移动时,你才能在网上找到大量的例子。
下一篇: Best practices for using the Entity Framework with WPF DataBinding