Search code examples
c#wpfeventsexpander

Adding multiple expanders event programmatically wont work


I've got a problem with adding Expanded event to my Expanders. I have expanders on my Window and I want to get the effect when I expand my expander all other will go down. I write functions that let me do this and it work correct. The problem is that I have 96 expanders I don't want add 96 events for Expand and 96 events for Collapse so I thought that I can add this programmatically.

look at the code:

    private void InitExpanders()
    {
        var expanders = GetExpanders(); // List<Expander> - list of expanders
        for (int i = 0; i < expanders.Count; i++)
        {
            if (i % 6 == 1)
            {
                    expanders[i - 1].Expanded += new RoutedEventHandler(delegate(object sender, RoutedEventArgs args)
                    {
                        DisableBigExpanders(1); // problem is here!
                    });
            }
        }
    }

this code works fine but for each expander function parameter will be 1. Ive tried to add integer and increment it but it wont works.

    private void InitExpanders()
    {
        var expanders = GetExpanders();
        int x = 0;
        for (int i = 0; i < expanders.Count; i++)
        {
            if (i % 6 == 1)
            {
                    expanders[i - 1].Expanded += new RoutedEventHandler(delegate(object sender, RoutedEventArgs args)
                    {
                        DisableBigExpanders(x);
                    });
                    x++;
            }
        }
    }

Thanks for all replies.


Solution

  • I suspect you are finding x in the delegate is always the highest value it reached during the loop. This is due to the way the compiler instantiates the instance of the anonymous method you've defined. It looks at what captured outer variables there are around the delegate and decides whether it requires separate instances or if it can use a single instance. In this case, you have no captured outer variables; therefore, the compiler is permitted to use the same instance of the delegate and therefore the same value of x.

    To get around this issue, all you need to do is add a more granular variable just before the delegate and assign it the value of x. This will work:

    private void InitExpanders()
    {
        var expanders = GetExpanders();
        int x = 0;
        for (int i = 0; i < expanders.Count; i++)
        {
            if (i % 6 == 1)
            {
                    int y = x++;
                    expanders[i - 1].Expanded += delegate
                    {
                        DisableBigExpanders(y);
                    };
            }
        }
    }
    

    See here for more info on the theory: http://en.csharp-online.net/ECMA-334%3A_14.5.15.4_Anonymous_method_evaluation