表单按钮不起作用

时间:2012-11-08 08:11:48

标签: wpf vb.net events button clicking

这是我的第一个问题,所以我会尽力说清楚!

我刚刚在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>

“Code-Behind”代码是:

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事件中放置任何命令,因为出现了这个问题。

到目前为止,在这个项目中,我还没有能够让我的任何按钮工作。我试过改变Window,Border和Grid的属性。我也尝试从Rectangle和Window中删除PreviewMouseLeftButtonDown事件无济于事。

非常感谢任何有关此问题的帮助!

1 个答案:

答案 0 :(得分:0)

那么你应该考虑史蒂夫发布的所有内容,如果你不知道确切的签名,请尽量不要手动创建事件处理程序。但是这里更大的问题是你处理整个窗口的鼠标预览事件(PreviewMouseLeftButtonDown="HandleHeaderPreviewMouseDown")和你调用Me.DragMove()的事件处理程序 - 它将事件标记为已处理,因此不再有鼠标事件处理程序要求儿童控制。首先阅读有关路由事件here的内容。您应该拨打Me.DragMove(),只有在按下并移动鼠标时,您才能在线找到大量示例。