user2243747
user2243747

Reputation: 2967

WPF label click event not getting fired

I have a label control and I have applied below style to this label so that it appears like a red round corner button.

<Style x:Key="LabelButton" TargetType="{x:Type Label}">
        <Setter Property="Background" Value="#ff6666" />
        <Setter Property="Foreground" Value="White"/>
        <Setter Property="FontFamily" Value="OpenSans"/>
        <Setter Property="FontSize" Value="25"/>
        <Setter Property="HorizontalContentAlignment" Value="Center"/>
        <Setter Property="VerticalContentAlignment" Value="Center"/>
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="{x:Type Label}">
                    <Border Name="Border" Background="#FF6666" BorderBrush="#FF6666" BorderThickness="1" CornerRadius="7" Padding="3">
                        <ContentPresenter Content="{TemplateBinding Content}" 
                                          ContentTemplate="{TemplateBinding ContentTemplate}" 
                                          VerticalAlignment="Center" 
                                          HorizontalAlignment="Center"/>                                                                                       
                    </Border>                        
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>

I have utilized one label and applied this style to that label and added handler for 'touch down'

<Label x:Name="lblNext" Style="{StaticResource LabelButton}" Grid.Row="4" Width="250" Height="75" PreviewTouchUp="lblNext_TouchDown">
            Next : Step 3
        </Label>

But event is not getting fired. I tried with all Preview events as well. Have I missed anything? I want to trigger TouchDown and mouse up event for this label.

Upvotes: 0

Views: 5816

Answers (1)

mgigirey
mgigirey

Reputation: 1103

You can try MVVM way to do it, of course if it is possible. Shouldn't be hard. I achieved the expected results using System.Windows.Interactivity.dll from Exression Blend.

In your XAML:

<Label x:Name="lblNext" Style="{StaticResource LabelButton}" Grid.Row="4" Width="250" Height="75" PreviewTouchUp="lblNext_TouchDown">
    <i:Interaction.Triggers>
        <i:EventTrigger EventName="MouseDown">
            <i:InvokeCommandAction Command="{Binding LabelClicked}" />
        </i:EventTrigger>
    </i:Interaction.Triggers>
    Next : Step 3
</Label>

I made available and example in https://github.com/mgigirey/WPFLabelClick.

Upvotes: 1

Related Questions