【问题标题】:Form buttons don't work表单按钮不起作用
【发布时间】:2012-10-28 09:49:15
【问题描述】:

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

在使用 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 事件,但无济于事。

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

【问题讨论】:

    标签: wpf vb.net events button clicking


    【解决方案1】:

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

    【讨论】:

    • 感谢 Novithchi S,我阅读了那篇文章并按照您的建议进行了快速搜索。我在 StackOverflow 上提出了这篇文章 - stackoverflow.com/questions/3592488/… 对于遇到同样问题的其他人,只需使用上面帖子中的代码进行最高控制,在我的例子中是“MainGrid”。
    猜你喜欢
    • 2011-05-03
    • 2015-10-10
    • 2016-09-18
    • 2011-02-10
    • 2013-03-09
    • 2020-08-21
    • 2015-04-29
    • 1970-01-01
    相关资源
    最近更新 更多