簡體   English   中英

WPF:按鈕控件模板不觸發點擊事件

[英]WPF: button control template not firing click event

我正在制作自己的服裝紐扣,上面有一個標簽。 出於某種原因,我的按鈕對'click'事件沒有反應,而click功能卻無法正常工作。 為什么?

這是我的按鈕樣式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>

使用Snoop的事件標簽來確定哪個控件正在處理您的點擊。

可能是以標簽獲得點擊的方式進行設置嗎? 您可以檢查將一些處理程序代碼綁定到標簽的Click事件。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM