【问题标题】:WPF: button control template not firing click eventWPF:按钮控件模板未触发单击事件
【发布时间】:2015-09-06 10:48:33
【问题描述】:

我正在制作自己的服装按钮,里面有一个标签。出于某种原因,我的按钮对“点击”事件没有反应,点击功能也没有午餐。为什么?

这是我的按钮样式 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>

【问题讨论】:

  • 复制了您的StyleClick 事件,每次单击Button 时都会触发。看来问题一定出在其他地方
  • 我也安装了 mahapps.metro。这可能是问题吗?
  • 当您更改模板时,您实际上更改了按钮的构成要素,因此我看不出 Mahapps 样式会如何影响它。顺便说一句,没有Rectangle 点击和所有鼠标事件,将仅适用于Label
  • 我修复了矩形的东西,但仍然没有。
  • 动画效果好吗?也许该按钮或其父按钮之一已被禁用。您的模板中没有任何内容可以指示该状态,因此您可能看不到它。

标签: c# wpf xaml


【解决方案1】:

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

【讨论】:

    【解决方案2】:

    您是否可以通过标签获得点击的方式进行设置?您可以检查将一些处理程序代码绑定到标签的Click 事件。

    【讨论】:

    • @789 你的自定义控件是派生自Button还是Control