views:

118

answers:

1

I have a UserControl with a ICommand-derived object as a Resource as follows:

<commands:SetRegionContentCommand x:Key="SetContentComand"/>

I then try to bind the command to the DataTemplate tag within an ItemsControl like so:

<ItemsControl ItemsSource="{Binding Path=Items}">
    <ItemsControl.ItemTemplate>                            
        <DataTemplate>
            <Button Grid.Column="1" Command="{Binding Source={StaticResource SetContentComand}}" CommandParameter="{Binding}">
                <Image Source="plus.jpeg" Width="16" Height="16"/>
            </Button>
         </...

However, when this happens I get "Element is already the child of another element" error messages in the browser. How can I prevent these errors from occurring?

I can see that the Command is being attached to the UserControl several times, but I want this to happen. How else does one bind commands to repeating databound controls?

A: 

Hi

Did you ever get an answer to this or a workaround. I have the same problem, ie. trying to bind to a staticresource command to a button in an ItemsControl.

Would really appreciate it if you figured it out, if you could let me know.

Many thanks

John

The workaround I used at the time was converting the repeating elements into a user control. The commands would also be defined within the user control. This would prevent the same command from being bound multiple times.
programatique