Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/284.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 在wpf树视图中查找parentitem_C#_Wpf_Data Binding_Xaml - Fatal编程技术网

C# 在wpf树视图中查找parentitem

C# 在wpf树视图中查找parentitem,c#,wpf,data-binding,xaml,C#,Wpf,Data Binding,Xaml,我希望who的答案能帮我解决这个问题,但没有。最初的情况基本相同: <TreeView ItemsSource="{Binding Groups}" Name="tvGroups" AllowDrop="True"> <TreeView.ItemTemplate> <HierarchicalDataTemplate ItemsSource="{Binding Participants}">

我希望who的答案能帮我解决这个问题,但没有。最初的情况基本相同:

<TreeView ItemsSource="{Binding Groups}" Name="tvGroups" AllowDrop="True">
          <TreeView.ItemTemplate>
                    <HierarchicalDataTemplate ItemsSource="{Binding Participants}">
                          <StackPanel Orientation="Horizontal" Drop="tvDrop" Tag="{Binding .}">
                               <TextBlock Text="{Binding Name}" />
                               <Button Tag="{Binding .}" Click="Button_Click_2">
                                    <Image Source="Resources/cross.png" />
                                </Button>
                          </StackPanel>
                          <HierarchicalDataTemplate.ItemTemplate>
                              <DataTemplate>
                                  <StackPanel Orientation="Horizontal" >
                                       <TextBlock Text="{Binding Alias}" />
                                       <Button Tag="{Binding .}" Name="btnDeleteParticipants" Click="btnParticipants_Click" >
                                            <Image Source="Resources/cross.png" />
                                       </Button>
                                  </StackPanel>
                              </DataTemplate>
                          </HierarchicalDataTemplate.ItemTemplate>
                    </HierarchicalDataTemplate>
          </TreeView.ItemTemplate>
</TreeView>
我想通过单击按钮(btnDeleteParticipants)从
组中删除
参与者p
。我试过这样的方法:

Control c = sender as Control;
while (!(c is TreeViewItem))
     c = (c.Parent) as Control;
但这没用(别问为什么,我不确定)。我可以通过检查
组是否包含
参与者
(绑定到
btndeleteAttements.Tag
)来找到
,但这将不允许参与者在多个组中。 那么,有没有办法找到合适的
团队

编辑:

Groups = new ObservableCollection<Group>();
Participants = new ObservableCollection<Participant>();
Groups=新的ObservableCollection();
参与者=新的可观察集合();

组和参与者是否可观察到收集对象

尝试使用以下方法:

static TObject FindVisualParent<TObject>(UIElement child) where TObject : UIElement
{
  if (child == null)
  {
    return null;
  }

  UIElement parent = VisualTreeHelper.GetParent(child) as UIElement;

  while (parent != null)
  {
    TObject found = parent as TObject;
    if (found != null)
    {
      return found;
    }
    else
    {
      parent = VisualTreeHelper.GetParent(parent) as UIElement;
    }
  }

  return null;
}
静态TObject findvisualpart(UIElement子级),其中TObject:UIElement
{
if(child==null)
{
返回null;
}
UIElement parent=VisualTreeHelper.GetParent(子)作为UIElement;
while(父级!=null)
{
TObject found=父对象为TObject;
如果(找到!=null)
{
发现退货;
}
其他的
{
parent=VisualTreeHelper.GetParent(parent)作为UIElement;
}
}
返回null;
}
另外,尝试使用DataContext获取参与者并将标记设置为TemplatedParent

<Button Tag="{Binding RelativeSource={RelativeSource TemplatedParent}}" />

然后点击

private void btnParticipants_Click(object sender, RoutedEventArgs e)
{
  var button = sender as Button;
  var p = button.DataContext as Participant;
  if (p == null) return;

  var t= FindVisualParent<TreeViewItem>(button); // get the Participants TreeViewItem
  if (t == null) return;
  var groupTreeItem = FindVisualParent<TreeViewItem>(t);  // get the groups TreeViewItem
  if (groupTreeItem == null) return;
  var group = groupTreeItem.DataContext as Group;
  group.Participants.Remove(p);
}
private void btnPartipants\u单击(对象发送方,路由目标)
{
var按钮=发送方为按钮;
var p=作为参与者的button.DataContext;
如果(p==null)返回;
var t=findvisualpart(按钮);//获取参与者树视图项
如果(t==null)返回;
var groupTreeItem=FindVisualParent(t);//获取组树项
if(groupTreeItem==null)返回;
var group=groupTreeItem.DataContext作为组;
组。参与者。移除(p);
}

组和参与者是否可观察到收集对象

尝试使用以下方法:

static TObject FindVisualParent<TObject>(UIElement child) where TObject : UIElement
{
  if (child == null)
  {
    return null;
  }

  UIElement parent = VisualTreeHelper.GetParent(child) as UIElement;

  while (parent != null)
  {
    TObject found = parent as TObject;
    if (found != null)
    {
      return found;
    }
    else
    {
      parent = VisualTreeHelper.GetParent(parent) as UIElement;
    }
  }

  return null;
}
静态TObject findvisualpart(UIElement子级),其中TObject:UIElement
{
if(child==null)
{
返回null;
}
UIElement parent=VisualTreeHelper.GetParent(子)作为UIElement;
while(父级!=null)
{
TObject found=父对象为TObject;
如果(找到!=null)
{
发现退货;
}
其他的
{
parent=VisualTreeHelper.GetParent(parent)作为UIElement;
}
}
返回null;
}
另外,尝试使用DataContext获取参与者并将标记设置为TemplatedParent

<Button Tag="{Binding RelativeSource={RelativeSource TemplatedParent}}" />

然后点击

private void btnParticipants_Click(object sender, RoutedEventArgs e)
{
  var button = sender as Button;
  var p = button.DataContext as Participant;
  if (p == null) return;

  var t= FindVisualParent<TreeViewItem>(button); // get the Participants TreeViewItem
  if (t == null) return;
  var groupTreeItem = FindVisualParent<TreeViewItem>(t);  // get the groups TreeViewItem
  if (groupTreeItem == null) return;
  var group = groupTreeItem.DataContext as Group;
  group.Participants.Remove(p);
}
private void btnPartipants\u单击(对象发送方,路由目标)
{
var按钮=发送方为按钮;
var p=作为参与者的button.DataContext;
如果(p==null)返回;
var t=findvisualpart(按钮);//获取参与者树视图项
如果(t==null)返回;
var groupTreeItem=FindVisualParent(t);//获取组树项
if(groupTreeItem==null)返回;
var group=groupTreeItem.DataContext作为组;
组。参与者。移除(p);
}

sry,我不知道如何使用这种方法。在parameterType(这个treevieItem项)之前的
这个
是怎么回事?我没有一个TreeViewItem用作参数。这是一个所谓的扩展方法。您可以在TReeViewItem上调用它,就像它是在TVI上声明的方法一样。您的XAML不起作用。我需要添加一个路径。(这个asp名称空间怎么了?)我删除了asp部分。它们不应该是必需的。sry。这一次是我的错。(我讨厌VS需要一个用于更新语法突出显示的构建)sry,我不知道如何使用这个方法。在parameterType(这个treevieItem项)之前的
这个
是怎么回事?我没有一个TreeViewItem用作参数。这是一个所谓的扩展方法。您可以在TReeViewItem上调用它,就像它是在TVI上声明的方法一样。您的XAML不起作用。我需要添加一个路径。(这个asp名称空间怎么了?)我删除了asp部分。它们不应该是必需的。sry。这一次我的坏。(我讨厌VS需要一个更新语法突出显示的构建)你可能会发现Josh Smith很有趣。你可能会发现Josh Smith很有趣。