Search code examples
silverlightbuttonchildwindowsender

Get sender (button) from child window


I was generate the button through code. and with the button i got one method to call up the child window in silverlight.

   private void btnXX_Click(object sender, RoutedEventArgs e)
    {
        Button btn = (Button)sender;
        SlotMaker slotMaker = new SlotMaker();
        slotMaker.Show();
    }

while the child window pop up, may i know how could i get the sender.content ?? from the child window


Solution

  • Modify the SlotMaker constructor to

     public DateTime SlotDateTime {get; private set; }
    
     public SlotMaker(DateTime slotDateTime)
     {
          SlotDateTime = slotDateTime;
          InitializeComponent();
          // Modify some display using value of SlotDateTime
     }
    

    Then in your button click

    private void btnXX_Click(object sender, RoutedEventArgs e)     
    {     
        Button btn = (Button)sender;
        DateTime dateTime = btn.Tag; // OR = DateTime.Parse(btn.Tag)     
        SlotMaker slotMaker = new SlotMaker(dateTime);     
        slotMaker.Show();     
    }
    

    Use the button Tag property to place a string in your Xaml that easily parses as .NET DateTime or use code to assign an actual DateTime to the Tag property.