2

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

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

非常感谢您对这个问题的任何帮助!

4

1 回答 1

0

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

于 2012-11-08T08:41:42.937 回答