WPF DataTemplate访问子级 [英] WPF DataTemplate Access Childs

查看:69
本文介绍了WPF DataTemplate访问子级的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嗨专家,



我在访问数据模板中定义的列表框时遇到问题。这是我的XAML:

Hi Experts,

I have a Problem accessing a Listbox which is defined in a datatemplate. Here is my XAML:

<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>





我想要做的是在单击ContextMenu复制项时获取所选的列表框项:

我认为这行代码应该为我管理所有内容并且应该返回当前所选TabItem的列表框:





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:

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





当我我在调用FindName我得到一个InvalidOperationException?

有没有人知道我做错了什么?



KR和谢谢

Manu



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

KR and Thanks
Manu

推荐答案

您收到该错误是因为您正在尝试访问未加载到控件中的控件。



您可以将所选项目绑定到MenuItem的 DataContext 标记。使用 RelativeSource 绑定来完成工作。在MenuItem的 Click 事件中,从发件人对象获取MenuItem并使用它。
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.


这篇关于WPF DataTemplate访问子级的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆