1

I created a checkbox list with 8 chechboxes added dynamically. The idea of my program is : when a box is checked, a chart appears on my plotter, when i uncheck it, it disappears.

My problem is that I dont know how to manage the events to do that because I added the checkboxes dynamically and I need 8 different events for 8 different charts.

Thanks.

4
  • 1
    Care to post your code? Commented Feb 15, 2013 at 16:36
  • 1
    Eventhandlers in WPF. I love me some Google. You should love you some Google too! Commented Feb 15, 2013 at 16:38
  • it wont help... I dont have a problem with the code. Commented Feb 15, 2013 at 16:44
  • 1
    @user2076231 if you do care about doing the things the right way in WPF, please let me know and I'll post a proper answer. Commented Feb 15, 2013 at 17:23

3 Answers 3

2

You can use one event for all of them. And inside of the event you will get the name of the control, which fired the event. Something like this:

 private void CheckBox_Click(object sender, RoutedEventArgs e)
 {
     CheckBox senderChk = sender as CheckBox;
     switch (senderChk.Name)
     {
         case "checkBox1":  //do something 
         case "checkBox2":  //do something 
     }
 }
Sign up to request clarification or add additional context in comments.

Comments

1

An answer here suggests using .Name property but for dynamically-created checkboxes that may now work well.

CheckBox chx;
chx.Tag = "Chart 1"; // put these tags in an enum or at least constants
chx.Click += chx_Click; 

void chx_Click(object sender, RoutedEventArgs e)
{
    CheckBox chx = sender as CheckBox;
    if (chx != null && chx.Tag != null)
    {
        switch (chx.Tag)
        {
            case "Chart 1": 
                        myChart1.Visibility = chx.IsChecked? Visibility.Visible: Visibility.Collapsed;  
                break;
            case "Chart 2": //...
                break;
            default:
                break;
        }
    }
}

3 Comments

Sorry but this is also a terribly bad solution, which uses WPF in an even worse winforms way. I mean, the Tag property? please...
Is the use of Tag property all you have against? I mean OP already abused WPF by dynamically creating checkboxes instead of defining them in XAML, possibly templated, binding the IsChecked to Visibility with a property converter. He asked a question, I answered, doesn't mean I agree with his design.
right. The supposed "dynamic" UI is the problem to begin with.
0

The "sender" parameter of the event handler indicates which control raised an event.

Somewhere, you create a control. Make sure you keep a reference to it somewhere, either as a member variable, in a dictionary or whatever.

Then, in your event handler, do the following:

If(sender==myControl)
{
   ...do something...
}
Elseif (sender==myOtherControl)
{
    ...do something else...
}

3 Comments

Sorry but this is a terribly bad solution, which uses WPF in a winforms way.
@HighCore, probably a fair comment, but please explain why this is a terrible way?
@ImmortalBlue because UI is Not Data. He should be using an ItemsControl with a proper DataTemplate to begin with, then he could program his application logic against Data Items instead of UI.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.