0

I am making my own costumed button, which has a label in it. For some reason, my button doesn't react to the 'click' event, and the click function doesn't lunch. why?

here is my button style xaml code:

   <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>

and this is the complete xaml code:

<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>

7
  • 1
    Copied your Style and Click event is fired each time I click the Button. It seems problem must be elsewhere Commented Sep 6, 2015 at 11:03
  • I also have mahapps.metro installed. could that be the problem? Commented Sep 6, 2015 at 11:19
  • When you change template you effectively change what makes the button so I don't see how Mahapps styles would affect it. BTW without the Rectangle click, and all mouse events, will work only on the Label Commented Sep 6, 2015 at 11:34
  • I fixed the rectangle thing, but still nothing. Commented Sep 6, 2015 at 11:50
  • Does the animation work? Maybe the button, or one of its parents, is disabled. There's nothing in your template to indicate that state so it's possible you wouldn't see it. Commented Sep 6, 2015 at 11:57

2 Answers 2

1

Use Snoop's events tab to determine which control is handling your click.

Sign up to request clarification or add additional context in comments.

Comments

0

Could it be you set something in a way that the label gets the click? You could check binding some handler-code to the Click event of the label.

1 Comment

@789 Is your your custom control derived from Button or from Control?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.