0

I've written a simple recycling control factory that is used to create and recycle controls. The idea is simple: the factory takes Func function as a parameter in constructor. Also it has an internal stack of FrameworkElements for recycling. GetControl method of the factory is implemented in obvious way: if there is a control in the stack, return it. In the other case, create a new control.

Imaging a simple situation. My control is just

<Border BorderBrush="Black" BorderThickness="5" Width="100" Height="30"
        Background="{Binding RelativeSource={RelativeSource AncestorType=StackPanel},
                             Path=Background}" />

There are two StackPanels with different backgrounds: the first is red, the second is green. I add a new control (using the factory) to the first stackpanel. It has a red background. I add a control to the second stackpanel. It has a green backgound. Then I remove the control from the first stackpanel. It's recycled. I add the control to the second StackPanel. But it has a red background! It seems that RelativeSource trigger wasn't updated.

Is the any way to handle the situation?

Clemens
  • 123,504
  • 12
  • 155
  • 268
  • I have an idea but I need to see more code. Show us how your add the recycled control to second StackPanel. – dev hedgehog Oct 25 '13 at 08:21
  • _stackPanel2.Children.Add(RecyclingControlFactory.GetControl()) – Alexander Boriskin Oct 25 '13 at 09:45
  • and how do you remove the control from old StackPanel? – dev hedgehog Oct 25 '13 at 09:46
  • var last = _stackPanel1.Children.LastOrDefault(); if (last != null) { RecyclingControlFactory.Recycle((FrameworkElement)last); _stackPanel1.Children.Remove(last); } – Alexander Boriskin Oct 25 '13 at 09:51
  • Ok, strange, the control should actually realize that it was disconnected from logical and visualtree or that it was added to. But try clearallbindings on a control before you add it to new StackPanel. Tell me then about your progress. http://msdn.microsoft.com/en-us/library/system.windows.data.bindingoperations.clearallbindings.aspx – dev hedgehog Oct 25 '13 at 10:16
  • I guess that would be a performance problem. According to http://stackoverflow.com/questions/4405288/removing-bindings-in-silverlight the only way to clear all bindings in silverlight is to use reflection. The way of manual clearing isn't convenient for me. The example described above is a simple demonstration of a problem we have in production code. It's absolutely impossible to clear all the bindings manually or via reflection. Nevertheless, thanks for your attempt. Any other ideas would be very appreciated. – Alexander Boriskin Oct 25 '13 at 16:02
  • Upload your complete code on github I would like to see where you got yourself into. :) – dev hedgehog Oct 26 '13 at 09:41

0 Answers0