[Edit: Rethinking architecture along mvvm lines made this problem largely fall away - thanks @kent]
Using Spring.NET + WPF.
Load two WPF buttons in the config:
<object name="Button1" type="System.Windows.Controls.Button, PresentationFramework" >
<property name="Name" value="Next" />
<property name="Width" value="200"/>
<property name="Content" value="Next"/>
</object>
<object name="Button2" type="System.Windows.Controls.Button, PresentationFramework" >
<property name="Name" value="Back" />
<property name="Width" value="200"/>
<property name="Content" value="Back"/>
</object>
and pass them into the constructor of a xaml class ("code behind")
<object name="MyNewScreen" type="MyControls.MyUserControl>
<constructor-arg name="buttons">
<list>
<ref object="Button1"/>
<ref object="Button2"/>
</list>
</object>
where the class constructor has:
public DataGridControl(ArrayList buttons)
{
//(yes this should be a List<Button> but I can't make spring happy with that)
foreach(var b in buttons) this.stackPanel.Children.Add(b);
...
Throws a:
Specified Visual is already a child of another Visual or the root of a CompositionTarget.
Now, I see the .GetHashCode() is the same for both buttons coming from Spring. So I can understand what WPF doesn't like.
But I can't do a XamlReader.Load(new XmlNodeReader(document));
as some have suggested, without losing my event wiring.
Any ideas how to get around this?
[Edit] I am wiring up events. Event wiring is not a problem, but it does explain why I'd want to do this in the first place. I'd left this out of the original post for the sake of brevity:
<object id="eventListener2" type="MyEventListener">
<listener event="Click" method="b_Click">
<ref object="Button2" />
</listener>
</object>