Juan Pablo Gomez
Juan Pablo Gomez

Reputation: 5534

Get WPF Name of shape at MouseDown Event

I have a user control with 7 shapes into it.

<UserControl x:Class="Gramas.OdontogramaUI.PiezaUI"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
             xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
             mc:Ignorable="d" 
             d:DesignHeight="280" Width="238.429" Name="EstaPieza" Margin="0,2" Loaded="EstaPieza_Loaded">
    <UserControl.Resources>
        <Style x:Key="Partes" TargetType="{x:Type Rectangle}"/>
    </UserControl.Resources>
    <Grid RenderTransformOrigin="0.5,0.457">
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="31*"/>
            <ColumnDefinition Width="45*"/>
            <ColumnDefinition Width="87*"/>
            <ColumnDefinition Width="44*"/>
            <ColumnDefinition Width="31*"/>
        </Grid.ColumnDefinitions>
        <Grid.RowDefinitions>
            <RowDefinition Height="53"/>
            <RowDefinition Height="36*"/>
            <RowDefinition Height="74*"/>
            <RowDefinition Height="41*"/>
            <RowDefinition Height="52*"/>
            <RowDefinition Height="24"/>
        </Grid.RowDefinitions>
        <Viewbox Margin="0,0,0,0" Stretch="Fill" Grid.RowSpan="5" Grid.ColumnSpan="5">
            <Grid Name="ContenedorPieza" Height="299.814" Width="238.429">
                <Path x:Name="Shape1" Data=""/>
                <Path x:Name="Shape2" Data=""/>
                <Path x:Name="Shape3" Data=""/>
                <Path x:Name="Shape4" Data=""/>
                <Path x:Name="Shape5" Data=""/>
                <Path x:Name="Shape6" Data=""/>
                <Path x:Name="Shape7" Data=""/>
            </Grid>
        </Viewbox>
        <TextBlock x:Name="NombrePieza" Text="{Binding ElementName=Esta_Pieza,Path=Pieza.Id}" Grid.Row="5" Margin="0" Height="Auto" Width="Auto" Grid.ColumnSpan="5" Panel.ZIndex="0" HorizontalAlignment="Center" VerticalAlignment="Center" Foreground="{DynamicResource {x:Static SystemColors.HotTrackBrushKey}}" FontWeight="ExtraBold"/>
    </Grid>
</UserControl>

Each shape is named using x:Shape1, I' want to control mousedown event into a single method

Shape1.MouseDown += Superficie_MouseDown;
Shape2.MouseDown += Superficie_MouseDown;
. . .

private void Superficie_MouseDown(object sender, MouseButtonEventArgs e)
{
    //some stuff here;
}  

My problem is I Can't Access the Name of the sender in my code behind, how can I do to identify the sender ? must handle it in separate methods.... one for each shape ?

Upvotes: 0

Views: 1270

Answers (1)

Rohit Vats
Rohit Vats

Reputation: 81253

Typecast sender to shape and access Name property of shape:

private void Superficie_MouseDown(object sender, MouseButtonEventArgs e)
{
    string name = ((Shape)sender).Name;
}

Upvotes: 1

Related Questions