Click here to Skip to main content
15,891,033 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hi Experts,

I have a Problem accessing a Listbox which is defined in a datatemplate. Here is my XAML:
XML
<Grid>
       <TabControl Name="tbCategories" Height="200" AllowDrop="True" VerticalAlignment="Top" HorizontalAlignment="Stretch" Grid.Column="1" ItemsSource="{Binding}" SelectionChanged="tbCategories_SelectionChanged">
           <TabControl.ItemTemplate>
               <DataTemplate>
                   <StackPanel Orientation="Horizontal" Margin="0" Name="spHeader">
                       <Grid Name="gdHeader">
                           <TextBox Name="tbCategoryNameEdit"  LostFocus="tbCategoryNameEdit_LostFocus" Text="{Binding Name}" removed="Transparent" BorderThickness="0" Visibility="Collapsed"/>
                           <TextBlock Name="tbCategoryName" MouseLeftButtonDown="tbCategoryName_MouseLeftButtonDown" Text="{Binding Name}" removed="Transparent" Visibility="Visible"/>
                       </Grid>
                       <Button Name="butDelete" Visibility="{Binding CanDelete, Converter={StaticResource boolvisibilityconverter}}" Content="x" VerticalContentAlignment="Center" Click="butDelete_Click" HorizontalContentAlignment="Center" Height="10" Width="10" FontSize="7" HorizontalAlignment="Right" VerticalAlignment="Top" Background="Transparent" BorderThickness="0" />
                   </StackPanel>
               </DataTemplate>
           </TabControl.ItemTemplate>

           <TabControl.ContentTemplate>
               <DataTemplate>
                   <ListBox AllowDrop="True" KeyDown="lbTemplates_KeyDown" ItemsSource="{Binding Templates}" BorderThickness="0" x:Name="lbTemplates" MouseDoubleClick="lbTemplates_MouseDoubleClick">
                       <ListBox.ContextMenu>
                           <ContextMenu>
                               <MenuItem Header="Copy" Name="mnuCopy" Click="mnuCopy_Click" />
                               <MenuItem Header="Paste" Name="mnuPaste" Click="mnuPaste_Click" />
                           </ContextMenu>
                       </ListBox.ContextMenu>
                   </ListBox>
               </DataTemplate>
           </TabControl.ContentTemplate>
       </TabControl>
   </Grid>


What i want to do is get the selected Listbox Item when Clicking the ContextMenu Copy Item:
I thought this line of code should manage all for me and should return me the Listbox of the current selected TabItem:

C#
tbCategories.SelectedContentTemplate.FindName("lbTemplates", tbCategories);


When I am calling FindName I get an InvalidOperationException?
Does anyone have an idea what I am doing wrong?

KR and Thanks
Manu
Posted

1 solution

You are getting that error because you are trying to access a control which is not loaded into the control.

You can binding the the selected item to the MenuItem's DataContext or Tag. Use RelativeSource Binding to do the job. And in Click event of the MenuItem get the MenuItem from the sender object and use it.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900