WPF c# change sender property in event

1.3k views Asked by At

Is it possible to change sender's property in event?

I have my own control in wpf with 10 Image controls. I set on all of them mouse enter and mouse leave events. All those events do the same(change size and Z index) but for specific Image.

With changing sender's property in event I will have only 2 event's methods, not 20. When I tried to change sender's property I saw it was readonly.

Is it possible to do ?

2

There are 2 answers

0
Tony Hopkinson On

Point all your controls at the same handlers. You can do this at design time or through code.

In the handler cast sender to the type of control.

Now when you change it's properties, you are changing the properties of the control that raised the event

PS don't forget to check to see if the cast is valid, before you try to access it's members.

0
BRAHIM Kamel On

Here how you can do it in xaml

/ <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }

        private void image1_MouseEnter(object sender, MouseEventArgs e)
        {
            //put your code here and all your images will points here 
        }

        private void image1_MouseLeave(object sender, MouseEventArgs e)
        {
             //put your code here and all your images will points here 
        }
    }
<Window x:Class="WpfApplication2.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="350" Width="525">
    <Grid>
        <Image Height="51" HorizontalAlignment="Left" Margin="91,116,0,0" Name="image1" Stretch="Fill" VerticalAlignment="Top" Width="61" MouseEnter="image1_MouseEnter" MouseLeave="image1_MouseLeave" />
        <Image Height="51" HorizontalAlignment="Left" Margin="91,116,0,0" Name="image2" Stretch="Fill" VerticalAlignment="Top" Width="61" MouseEnter="image1_MouseEnter" MouseLeave="image1_MouseLeave" />
        <Image Height="51" HorizontalAlignment="Left" Margin="91,116,0,0" Name="image3" Stretch="Fill" VerticalAlignment="Top" Width="61" MouseEnter="image1_MouseEnter" MouseLeave="image1_MouseLeave" />
        <Image Height="51" HorizontalAlignment="Left" Margin="91,116,0,0" Name="image4" Stretch="Fill" VerticalAlignment="Top" Width="61" MouseEnter="image1_MouseEnter" MouseLeave="image1_MouseLeave" />
    </Grid>
</Window>

//and here in code behind

public partial class MainWindow : Window
{
    public MainWindow()
    {
        InitializeComponent();
        image1.MouseEnter += image1_MouseEnter;
        image2.MouseEnter += image1_MouseEnter;
        image3.MouseEnter += image1_MouseEnter;
        image4.MouseEnter += image1_MouseEnter;


        image1.MouseEnter += image1_MouseLeave;
        image2.MouseEnter += image1_MouseLeave;
        image3.MouseEnter += image1_MouseLeave;
        image4.MouseEnter += image1_MouseLeave;

    }

    private void image1_MouseEnter(object sender, MouseEventArgs e)
    {

    }

    private void image1_MouseLeave(object sender, MouseEventArgs e)
    {

    }
}