Form buttons don't work
This is my first question, so I'll do my best to make it clear!
I am just starting up in WPF after several years in WinForms, and for the life of me, I can't get my buttons' events to trigger. All I currently have in the project is a border-less window with a custom rectangle for the title bar. The XAML code is:
<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>
And the "Code-Behind" code is:
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
As you can see, I still haven't put any commands in the MouseLeave and MouseEnter events because this problem has come up.
So far with this project, I have not once been able to get any of my buttons to work. I have tried changing the properties of the Window, Border and Grid. I have also tried removing the PreviewMouseLeftButtonDown events from both the Rectangle and the Window to no avail.
Any help with this problem is greatly appreciated!
Well you should consider all what Steve has posted and try not to create the eventhandlers manually if you don't know the exact signature. But the bigger problem here is that you handle mouse preview event ( PreviewMouseLeftButtonDown="HandleHeaderPreviewMouseDown"
) for your entire window and in the event handler you call Me.DragMove()
- which marks the event as handled, so no more mouse eventhandlers will be called for child controls. First read something about routed events here. You should call the Me.DragMove()
, only if the mouse was pressed and moved, you can find a ton of examples online.
上一篇: Winforms到WPF模式
下一篇: 表单按钮不起作用