WPF获取父类控件信息

xtzy89 xtzy89 墨衍会员 · AI 创作全网分发 墨衍智能分发 通过墨衍一键同步至各平台 可同步平台 CSDN微信公众号微博知乎掘金百家号博客园抖音小红书 你的文章也可以这样分发 墨衍会员 ¥399起/年,写一次发全网 我也要 + 加入社区

点击"APTIV-07"时如何获取"Project_V3"的名称,现在只能获取上一级的名称,无法获得上上级。

XAML格式为:

<DockPanel.Resources>
<local:Data x:Key="MyList"/>

<HierarchicalDataTemplate DataType = "{x:Type local:Data}"
ItemsSource = "{Binding Path= Versions}">

<TextBlock Text="{Binding Path=ProjectName}"/>
</HierarchicalDataTemplate>

<HierarchicalDataTemplate DataType = "{x:Type local:Version}"
ItemsSource = "{Binding Path=Machines}">
<TextBlock Text="{Binding Path=Ver_Name}"/>
</HierarchicalDataTemplate>

<DataTemplate DataType="{x:Type local:Machine}">
<TextBlock Text="{Binding Path=MachineName}" MouseLeftButtonDown="TextBlock_MouseLeftButtonDown" />
</DataTemplate>
</DockPanel.Resources>

<Menu x:Name="menu" DockPanel.Dock="Top" VerticalAlignment="Top" Visibility="Collapsed"
GotKeyboardFocus="menu_GotKeyboardFocus"
LostKeyboardFocus="menu_LostKeyboardFocus"
ContextMenuOpening="menu_ContextMenuOpening">
<MenuItem Header="Project"
ItemsSource="{Binding Projects}" >
</MenuItem>
</Menu>


获取上级目录用的方法是:

public static T GetParent<T>(DependencyObject obj) where T : FrameworkElement
{
if (obj != null)
{
DependencyObject parent = VisualTreeHelper.GetParent(obj);
while (parent != null)
{
if (parent is T)
{
return (T)parent;
}
parent = VisualTreeHelper.GetParent(parent);
}
}
return null;
}

//获取上级
var parent = GetParent<StackPanel>(e.OriginalSource as DependencyObject);
string currentVersion = ((Version)parent.DataContext).Ver_Name;


有没有大神帮忙看下,如何获取上上级"Project_V3"这个字段,后边显示的时候需要用到。
...全文

相关推荐

评论 1
收藏 已收藏
分享
返回顶部
xtzy89
C# 111,129 成员 642,540 内容