wonyh_
wonyh_

Reputation: 141

Fire click on templated button

I want to create a custom button reveal template. I won't use Style={ThemeResource ButtonRevealStyle} for some reason. So, I've added ListViewItem inside button control template.

<Style TargetType="Button" x:Key="ButtonListView">
    <Setter Property="IsTabStop" Value="False"/>
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="Button">
                <Grid Control.IsTemplateFocusTarget="True">
                    <StackPanel>
                        <ListViewItem>
                            <ContentPresenter></ContentPresenter>
                        </ListViewItem>
                    </StackPanel>
                </Grid>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>


<Button Style="{ThemeResource ButtonListView}" Click="list_refresh">
    <TextBlock Text="Button Content"/>
</Button>

Illustration: Button template illustration

But click event doesn't fire when I clicked on it. I think it fires on ListViewItem, not the button. And the most confusing thing is, it could fires using keyboard.

Upvotes: 1

Views: 95

Answers (2)

Nico Zhu
Nico Zhu

Reputation: 32775

Fire click on templated button

As @Vincent said ListViewItem is the first one to capture your click event, for your scenario, we suggest you custom button reveal style and use ListViewItem reveal theme resource. Or use Button Tapped event to replace.

For exampl:

<Style x:Key="CudtomButtonRevealStyle" TargetType="Button">
    <Setter Property="Background" Value="{ThemeResource ListViewItemRevealBackground}" />
    <Setter Property="Foreground" Value="{ThemeResource ButtonForeground}" />
    <Setter Property="BorderBrush" Value="{ThemeResource ListViewItemRevealBorderBrush}" />
    <Setter Property="BorderThickness" Value="{ThemeResource ButtonRevealBorderThemeThickness}" />
    <Setter Property="Padding" Value="{ThemeResource ButtonPadding}" />
    <Setter Property="HorizontalAlignment" Value="Left" />
    <Setter Property="VerticalAlignment" Value="Center" />
    <Setter Property="FontFamily" Value="{ThemeResource ContentControlThemeFontFamily}" />
    <Setter Property="FontWeight" Value="Normal" />
    <Setter Property="FontSize" Value="{ThemeResource ControlContentThemeFontSize}" />
    <Setter Property="UseSystemFocusVisuals" Value="{StaticResource UseSystemFocusVisuals}" />
    <Setter Property="FocusVisualMargin" Value="-3" />
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="Button">
                <Grid
                    x:Name="RootGrid"
                    Background="{TemplateBinding Background}"
                    CornerRadius="{TemplateBinding CornerRadius}">
                    <ContentPresenter
                        x:Name="ContentPresenter"
                        Padding="{TemplateBinding Padding}"
                        HorizontalContentAlignment="{TemplateBinding HorizontalContentAlignment}"
                        VerticalContentAlignment="{TemplateBinding VerticalContentAlignment}"
                        AutomationProperties.AccessibilityView="Raw"
                        BorderBrush="{TemplateBinding BorderBrush}"
                        BorderThickness="{TemplateBinding BorderThickness}"
                        Content="{TemplateBinding Content}"
                        ContentTemplate="{TemplateBinding ContentTemplate}"
                        ContentTransitions="{TemplateBinding ContentTransitions}"
                        CornerRadius="{TemplateBinding CornerRadius}" />

                    <VisualStateManager.VisualStateGroups>
                        <VisualStateGroup x:Name="CommonStates">
                            <VisualState x:Name="Normal">

                                <Storyboard>
                                    <PointerUpThemeAnimation Storyboard.TargetName="RootGrid" />
                                </Storyboard>
                            </VisualState>

                            <VisualState x:Name="PointerOver">
                                <VisualState.Setters>
                                    <Setter Target="RootGrid.(RevealBrush.State)" Value="PointerOver" />
                                    <Setter Target="RootGrid.Background" Value="{ThemeResource ListViewItemRevealBackgroundPointerOver}" />
                                    <Setter Target="ContentPresenter.BorderBrush" Value="{ThemeResource ListViewItemRevealBorderBrushPointerOver}" />
                                    <Setter Target="ContentPresenter.Foreground" Value="{ThemeResource ButtonForegroundPointerOver}" />
                                </VisualState.Setters>

                                <Storyboard>
                                    <PointerUpThemeAnimation Storyboard.TargetName="RootGrid" />
                                </Storyboard>
                            </VisualState>

                            <VisualState x:Name="Pressed">
                                <VisualState.Setters>
                                    <Setter Target="RootGrid.(RevealBrush.State)" Value="Pressed" />
                                    <Setter Target="RootGrid.Background" Value="{ThemeResource ListViewItemRevealBackgroundPressed}" />
                                    <Setter Target="ContentPresenter.BorderBrush" Value="{ThemeResource ListViewItemRevealBorderBrushPressed}" />
                                    <Setter Target="ContentPresenter.Foreground" Value="{ThemeResource ButtonForegroundPressed}" />
                                </VisualState.Setters>

                                <Storyboard>
                                    <PointerDownThemeAnimation Storyboard.TargetName="RootGrid" />
                                </Storyboard>
                            </VisualState>

                            <VisualState x:Name="Disabled">
                                <VisualState.Setters>
                                    <Setter Target="RootGrid.Background" Value="{ThemeResource ButtonRevealBackgroundDisabled}" />
                                    <Setter Target="ContentPresenter.BorderBrush" Value="{ThemeResource ButtonRevealBorderBrushDisabled}" />
                                    <Setter Target="ContentPresenter.Foreground" Value="{ThemeResource ButtonForegroundDisabled}" />
                                </VisualState.Setters>
                            </VisualState>
                        </VisualStateGroup>
                    </VisualStateManager.VisualStateGroups>
                </Grid>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>

Usage

<Button
    VerticalAlignment="Bottom"
    Click="Button_Click"
    Content="Connect"
    Style="{ThemeResource CudtomButtonRevealStyle}" />

Upvotes: 2

Vincent
Vincent

Reputation: 3294

This is the focus problem. When you use a keyboard, you could use Tab to move focus to Button, so it fires the click event.

When you use a pointer, and press on the Button, ListViewItem is the first one to capture your click event, you can add a Tapped event to ListViewItem to prove it.

So If you insist want the Button to respond to your pointer, I think you can remove the ListViewItem.

   <ControlTemplate TargetType="Button">
        <Grid Control.IsTemplateFocusTarget="True">
            Add button content here
        </Grid>
    </ControlTemplate>

Upvotes: 1

Related Questions