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

时间:2015-09-06 10:48:33

标签: c# wpf xaml

我正在制作我自己的服饰按钮,其中有一个标签。出于某种原因,我的按钮不会对“点击”事件做出反应,并且点击功能不会午餐。为什么呢?

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

2 个答案:

答案 0 :(得分:1)

使用Snoop的事件标签确定处理点击的控件。

答案 1 :(得分:0)

你是否可以通过标签获取点击的方式设置某些内容?您可以检查将某些处理程序代码绑定到标签的Click事件。

相关问题