Home > DeveloperSection > Forums > How to change an event's sender in WPF/XAML
jayprakash sharma
jayprakash sharma

Total Post:117

Points:821
Posted on    December-27-2013 6:47 AM

 WPF WPF 
Ratings:


 1 Reply(s)
 1595  View(s)
Rate this:

I want to set the sender of an event to something other than the actual initiator of the event. Consider this simple DataTemplate for my CImage class:

<DataTemplate DataType="{x:Type er:CImage}">

    <Grid VerticalAlignment="Center" HorizontalAlignment="Center" Margin="5">

        <StackPanel Orientation="Vertical" MaxWidth="{Binding Image.Width}">

            <Image Source="{Binding Image}" MouseUp="MouseUpHandler" />

            <Label Content="{Binding ImageFile.Name}" />

            <Label>

                <TextBlock  TextWrapping="WrapWithOverflow" Text="{Binding TagsJoined}" />

            </Label>

        </StackPanel>

    </Grid>

</DataTemplate>

When the user clicks on this:

<Image Source="{Binding ImageSource}" MouseUp="MouseUpHandler" />

The "MouseUpHandler" event handler needs access to the "Image" object of which "ImageSource" is a property, not the "Image" object to which "ImageSource" has been bound.

Is there any way to communicate this information in the event itself or do I have no choice but to have the handler look up the Image through other means?


Alex Leblois
Alex Leblois

Total Post:67

Points:471
Posted on    December-27-2013 7:36 AM

Hi Jay,

Sender will always be Image object, that you can't change.

However, in handler you can access DataContext of Image which will be object of CImage since DataType of DataTemplate is CImage:

private void MouseUpHandler(object sender, MouseButtonEventArgs e)

{

    CImage instance = (CImage)((Image)sender).DataContext;

}


Don't want to miss updates? Please click the below button!

Follow MindStick