ListBox ContextMenu Error

ListBox ContextMenu Error

Old forum URL:

Ranjini posted on Friday, June 25, 2010

Tools & Methodologies - .NET 4, Silverlight 4, CSLA 4, MVVM


I need to add a ContextMenu to every item on a ListBox and associate each action item on the ContextMenu with a CSLA trigger action invoking a method in a ViewModel. Here is how I have the XAML (simplified) laid out in an attempt to do this.


<ListBox x:Name="lbAssetSummaryTemplate" DataContext="{Binding DomainAssetSummaryTemplate, Mode=TwoWay}" ItemsSource="{Binding}" >



             <StackPanel Orientation="Horizontal">

                <TextBlock Text="{Binding Name}"/>


                        <controlsInputToolkit:ContextMenu x:Name="itemMenu">

                            <controlsInputToolkit:MenuItem Header="Move Up" x:Name="btnMoveUp"/>

<csla:TriggerAction Height="0" Name="MoveASTFUp" Width="0"  DataContext="{StaticResource domainEditModel}" TargetControl="{Binding ElementName=btnMoveUp}" MethodName="MoveAssetSummaryTemplateFieldUp" MethodParameter="{Binding ElementName=lbAssetSummaryTemplate, Path=SelectedItem}" />                            








However, this seems to be causing some unwanted issues. When I first load the screen, I get this warning -

The entire error reads  -

Error: Unhandled Error in Silverlight Application

Code: 4009   

Category: ManagedRuntimeError      

Message: Element is already the child of another element.    

If I click No I am able to proceed and everything else works fine. Anyone have any idea what I am doing wrong?



RockfordLhotka replied on Friday, June 25, 2010

Try with beta 3, there was a bug in TriggerAction that caused trouble in a DataTemplate, and I wonder if you are hitting the same issue.

Ranjini replied on Friday, June 25, 2010

I was excited about your suggestion and installed beta 3. Unfortunately, I am still getting the error.

RockfordLhotka replied on Friday, June 25, 2010

In that case, can you provide an extremely simple solution that demonstrates the problem?

Ranjini replied on Tuesday, June 29, 2010

Ok.. This might be a very useless posting, but I have resolved the problem. I wanted to create a sample solution to demonstrate the problem and was using the CslaMVVMSl solution to modify it in a manner that resembled my page. In doing so, I figured that with some work, I could get it work perfectly fine in that solution. I compared it to mine and realized the only thing I had different in that peice was, I had DataContext={StaticResource ViewModel} and the working solution had DataContext={Binding Source={StaticResource ViewModel}}. I was excited that this would fix my problem but when i made this change in my solution , I realised I celebrated too soon. It had no effect on my problem.

After several hours worth of small changes, I figured out that it all came down to the DataContext tag and how it was being inherited/set. Visually and syntactically it all looked right, but since my page was very complicated with 3 viewmodels governing peices of it, somewhere there was a broken/missing peice.

So, I systematically analyzed top-down every DataContext tag and made sure I eliminated redundancies and streamlined it. I also made each one of the DataContext tags a binding (ie DataContext={Binding Source={StaticResource ViewModel}}) rather than a direct assignment (ie DataContext={StaticResource ViewModel}}) and it worked!!! Unfortunately, I cannot point my finger on what solved the problem and am not even sure at this point if it was a problem with something I did or a bug that I just managed to circumvent somehow. If there is anything I can answer to make this determination, please let me know.

In the mean time, my only advice to anyone getting this error is - track your DataContext tags !


RockfordLhotka replied on Tuesday, June 29, 2010

Troubleshooting the DataContext is way too hard in my opinion - and the problem you describe is the biggest issue I keep facing when building SL/WPF apps.

Copyright (c) Marimer LLC