WPF:按钮控制模板不触发点击事件

本文关键字:事件 按钮 控制 WPF | 更新日期: 2023-09-27 18:11:19

我正在制作我自己的服装按钮,其中有一个标签。出于某种原因,我的按钮没有对"点击"事件做出反应,点击功能也没有发挥作用。为什么?

这是我的按钮样式xaml代码:

   <Style x:Key="MyMenuButtonStyle" TargetType="{x:Type Button}">
        <Setter Property="Background" Value="#AF4EB4EC"/>
        <Setter Property="Foreground"  Value="White"/>
        <Setter Property="BorderThickness" Value="0"/>
        <Setter Property="Margin" Value="0,0,5,0"/>
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="{x:Type Button}">
                    <Grid >
                        <Rectangle Fill="{TemplateBinding Background}"  StrokeThickness="0" MinWidth="{Binding ElementName=lblCnt,Path=Width}"/>
                        <Label Name="lblCnt" Content="{TemplateBinding Content}"  HorizontalAlignment="Center" VerticalAlignment="Center" 
                               Foreground="{TemplateBinding Foreground}" FontSize="{TemplateBinding FontSize}" Background="{TemplateBinding Background}"
                               Height="auto" Width="auto"/>
                    </Grid>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
        <Style.Triggers>
            <Trigger Property="IsMouseOver" Value="True">
                <Setter Property="Cursor" Value="Hand"/>
                <Setter Property="Background" Value="White"/>
            </Trigger>
            <EventTrigger RoutedEvent="UIElement.MouseEnter">
                <BeginStoryboard >
                    <Storyboard>
                        <!--<DoubleAnimation Storyboard.TargetProperty="Width" 
                                                        Duration="0:0:0.200" By="30"/>-->
                        <DoubleAnimation Storyboard.TargetProperty="FontSize"
                                                        Duration="0:0:0.200"
                                                         From="12" To="22"/>
                        <ColorAnimation Storyboard.TargetProperty="(Control.Background).(SolidColorBrush.Color)"
                                                        Duration="0:0:0.200"
                                                        From="#AF4EB4EC" To="White"/>
                        <ColorAnimation Storyboard.TargetProperty="(Control.Foreground).(SolidColorBrush.Color)"
                                                        Duration="0:0:0.200"
                                                        From="White" To="#AF4EB4EC"/>
                    </Storyboard>
                </BeginStoryboard>
            </EventTrigger>
            <EventTrigger RoutedEvent="UIElement.MouseLeave">
                <BeginStoryboard>
                    <Storyboard >
                        <!--<DoubleAnimation Storyboard.TargetProperty="Width"
                                                         Duration="0:0:0.100"
                                                         By="-30"/>-->
                        <DoubleAnimation Storyboard.TargetProperty="FontSize"
                                                         Duration="0:0:0.100"
                                                         From="22" To="12"/>
                        <ColorAnimation Storyboard.TargetProperty="(Control.Background).(SolidColorBrush.Color)"
                                                        Duration="0:0:0.100"
                                                        From="White"
                                                        To="#AF4EB4EC"/>
                        <ColorAnimation Storyboard.TargetProperty="(Control.Foreground).(SolidColorBrush.Color)"
                                                        Duration="0:0:0.100"
                                                        From="#AF4EB4EC"
                                                        To="White"/>
                    </Storyboard>
                </BeginStoryboard>
            </EventTrigger>
        </Style.Triggers>
    </Style>

这是完整的xaml代码:

<Window x:Class="FMS_Csharp_GUI.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="File Indexer" Height="552" Width="864" Icon="magna-folder-icon.ico">
<Window.CommandBindings>
    <CommandBinding Command="New" CanExecute="CanExeNewRepo" Executed="ClickNewRepo"/>
    <CommandBinding Command="Delete" CanExecute="CanExeNewRepo" Executed="ClickDeleteRepo"/>
</Window.CommandBindings>
<Window.Resources>
    <StackPanel x:Key="documentStackPanelStyle"  Orientation="Horizontal" >
        <Image Source="Images/openDoc.ico" Width="32" Height="32"/>
        <Label Content="" VerticalAlignment="Center"/>
    </StackPanel>
    <Style x:Key="MyMenuButtonStyle" TargetType="{x:Type Button}">
        <Setter Property="Background" Value="#AF4EB4EC"/>
        <Setter Property="Foreground"  Value="White"/>
        <Setter Property="BorderThickness" Value="0"/>
        <Setter Property="Margin" Value="0,0,5,0"/>
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="{x:Type Button}">
                    <Grid >
                        <Rectangle Fill="{TemplateBinding Background}"  StrokeThickness="0" MinWidth="{Binding ElementName=lblCnt,Path=Width}"/>
                        <Label Name="lblCnt" Content="{TemplateBinding Content}"  HorizontalAlignment="Center" VerticalAlignment="Center" 
                               Foreground="{TemplateBinding Foreground}" FontSize="{TemplateBinding FontSize}" Background="{TemplateBinding Background}"
                               Height="auto" Width="auto"/>
                    </Grid>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
        <Style.Triggers>
            <Trigger Property="IsMouseOver" Value="True">
                <Setter Property="Cursor" Value="Hand"/>
                <Setter Property="Background" Value="White"/>
            </Trigger>
            <EventTrigger RoutedEvent="UIElement.MouseEnter">
                <BeginStoryboard >
                    <Storyboard>
                        <!--<DoubleAnimation Storyboard.TargetProperty="Width" 
                                                        Duration="0:0:0.200" By="30"/>-->
                        <DoubleAnimation Storyboard.TargetProperty="FontSize"
                                                        Duration="0:0:0.200"
                                                         From="12" To="22"/>
                        <ColorAnimation Storyboard.TargetProperty="(Control.Background).(SolidColorBrush.Color)"
                                                        Duration="0:0:0.200"
                                                        From="#AF4EB4EC" To="White"/>
                        <ColorAnimation Storyboard.TargetProperty="(Control.Foreground).(SolidColorBrush.Color)"
                                                        Duration="0:0:0.200"
                                                        From="White" To="#AF4EB4EC"/>
                    </Storyboard>
                </BeginStoryboard>
            </EventTrigger>
            <EventTrigger RoutedEvent="UIElement.MouseLeave">
                <BeginStoryboard>
                    <Storyboard >
                        <!--<DoubleAnimation Storyboard.TargetProperty="Width"
                                                         Duration="0:0:0.100"
                                                         By="-30"/>-->
                        <DoubleAnimation Storyboard.TargetProperty="FontSize"
                                                         Duration="0:0:0.100"
                                                         From="22" To="12"/>
                        <ColorAnimation Storyboard.TargetProperty="(Control.Background).(SolidColorBrush.Color)"
                                                        Duration="0:0:0.100"
                                                        From="White"
                                                        To="#AF4EB4EC"/>
                        <ColorAnimation Storyboard.TargetProperty="(Control.Foreground).(SolidColorBrush.Color)"
                                                        Duration="0:0:0.100"
                                                        From="#AF4EB4EC"
                                                        To="White"/>
                    </Storyboard>
                </BeginStoryboard>
            </EventTrigger>
        </Style.Triggers>
    </Style>
</Window.Resources>
<Grid>
    <Grid x:Name="grdContents" Margin="0,23,0,76">
        <Grid Name="grdUserInfo" HorizontalAlignment="Right" Height="50" VerticalAlignment="Top" Width="189" Margin="0,-18,0,0">
            <Image Name="imgUser" Source="Images/notloggedin.jpg" HorizontalAlignment="Right" Height="49.667" VerticalAlignment="Top"/>
            <Label Name="lblUser" Content="you are not logged in!" Margin="0,0,54.667,0" HorizontalAlignment="Right" Width="134.333" Height="49.667" VerticalAlignment="Top"/>
        </Grid>
        <StackPanel Name="stkpMyStyledMenu" Height="47" Margin="0,10,0,0" Background="#FF4EA2EC" Orientation="Horizontal" VerticalAlignment="Top" HorizontalAlignment="Left">
            <StackPanel.Resources>
                <Style BasedOn="{StaticResource MyMenuButtonStyle}" TargetType="Button"/>
            </StackPanel.Resources>
            <Button  Content="New Repo" Click="myMenuNewRepo"/>
            <Button Content="Open Repo" Click="MyMenuClickOpenRepo" />
            <Button  Content="Login" Click="MyMenuClickLogin"/>
            <Button Content="Exit"  Click="MyMenuClickExit"/>

        </StackPanel>
        <GroupBox Header="Input" Name="grpInput" HorizontalAlignment="Left" Margin="145,0,0,120" VerticalAlignment="Bottom" RenderTransformOrigin="0.352,-0.304" Height="185" Width="290">
        </GroupBox>
        <Button Content="Button" HorizontalAlignment="Left" Margin="205,70,0,0" VerticalAlignment="Top" Width="75" Click="MyMenuClickNewRepo"/>
        <Button Content="Button"  Click="MyMenuClickOpenRepo" HorizontalAlignment="Left" Margin="285,70,0,0" VerticalAlignment="Top" Width="75"/>
    </Grid>
    <ListBox Name="lstMessages"  Height="61" VerticalAlignment="Bottom"/>

</Grid>

WPF:按钮控制模板不触发点击事件

使用Snoop的events选项卡来确定哪个控件正在处理您的单击。

是不是你设置了一些东西让标签得到点击?您可以检查将一些处理程序代码绑定到标签的Click事件。