locked
"this" when event is fired. RRS feed

  • Question

  • Hi All, 

    I have some XAML that looks as below. I have a SizeChanged="Button_SizeChanged" attribute on Button.

    When the Button_SizeChanged method is called, I would have expected "this.name" to be Button 1, but instead it is xWindow1 - indicating that "this" is the Window, not the button.

    How can I get a reference the item that the event was fired on (Button1 in this case)?

    <Window
        Name="xWindow1"
        x:Class="Clipping_mask.Window1"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="Window1" Height="300" Width="300">
        <Grid Name="Grid1">
            <Button Name="Button1" ClipToBounds="True" Margin="100,100,100,100" SizeChanged="Button_SizeChanged" >
                <Image Name="image1" Stretch="None" Source="C:\Documents and Settings\wfitzgerald\My Documents\My Pictures\Required Area\1000x1000.jpg" Margin="-100"></Image>
            </Button>

        </Grid>
    </Window>

     

    Thanks
    Warrick

    Monday, January 26, 2009 2:20 PM

Answers

  • You can get the button by referencing the name:

    Button1.Name

    Or casting the sender parameter:

    ((Button)sender).Name

    • Marked as answer by Wozza99 Monday, January 26, 2009 3:25 PM
    Monday, January 26, 2009 2:34 PM
  • Oj, never mind, I see - I need to do something like this:

     

    Button srcButton = (Button)e.Source;

    srcButton.Width = Calc

     

    Thanks

    Warrick

    • Marked as answer by Wozza99 Monday, January 26, 2009 3:25 PM
    Monday, January 26, 2009 3:24 PM

All replies

  • You can get the button by referencing the name:

    Button1.Name

    Or casting the sender parameter:

    ((Button)sender).Name

    • Marked as answer by Wozza99 Monday, January 26, 2009 3:25 PM
    Monday, January 26, 2009 2:34 PM
  • Thanks, but I'm not sure if that's going to help me. 

    I plan to have this method called from multiple area - the method will do some calculations and then adjust the width of the item that fired the event.

    So let's say I had 3 buttons all with this property SizeChanged="Button_SizeChanged".

    I want to do something like this:

    double Calc = XXXXX

     (Button)sender.Width = Calc

     

    I can't seem to cast sender into any usable format though?

    Monday, January 26, 2009 3:19 PM
  • The sender is your button.

    Button button = (Button)sender;

    button.Width = whatever;


    Controls for WPF, Windows Forms and Silverlight at http://www.divelements.co.uk
    • Proposed as answer by DutchMarcel Tuesday, January 27, 2009 9:23 AM
    Monday, January 26, 2009 3:20 PM
  • Oj, never mind, I see - I need to do something like this:

     

    Button srcButton = (Button)e.Source;

    srcButton.Width = Calc

     

    Thanks

    Warrick

    • Marked as answer by Wozza99 Monday, January 26, 2009 3:25 PM
    Monday, January 26, 2009 3:24 PM