【问题标题】:Find parentitem in wpf-treeview在 wpf-treeview 中查找父项
【发布时间】:2009-03-23 17:42:10
【问题描述】:

我希望my previous question 的答案能帮助我解决这个问题,但没有。最初的情况几乎相同:

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

 private void btnParticipants_Click(object sender, RoutedEventArgs e)//Probanten aus Gruppe entfernen
        {
            Participant p = ((sender as Button).Tag as Participant);
            if (p == null) return;
            //TODO: Raus bekommen in welcher Gruppe ich löschen will

        }

我想通过单击按钮 (btnDeleteParticipants) 从 Group 中删除 Participant p。我试过这样的:

Control c = sender as Control;
while (!(c is TreeViewItem))
     c = (c.Parent) as Control;

但这不起作用(不要问为什么,我不确定)。我可以通过检查它是否包含Participant(绑定到btnDeleteParticipants.Tag)来找到Group,但这将不允许参与者加入超过1个组。 那么,任何想法如何获得正确的Group

编辑:

Groups = new ObservableCollection<Group>();
Participants = new ObservableCollection<Participant>();

【问题讨论】:

  • 你可能会觉得 Josh Smith 的 this article 很有趣。

标签: c# wpf data-binding xaml


【解决方案1】:

组和参与者是 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;
}

另外,尝试使用 DataContext 获取参与者并将 Tag 设置为 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);
}

【讨论】:

  • sry,我不知道如何使用这个方法。在 ParamterType(this TreeViewItem 项)之前,this 怎么了?我没有用作参数的 TreeViewItem。
  • 这就是所谓的扩展方法。您可以在 TReeViewItem 上调用它,就好像它是在 TVI 上声明的方法一样。
  • 您的 XAML 不起作用。我需要添加一个路径。(这个 asp 命名空间是怎么回事?)
  • 我删除了 asp 部分。他们的路径不应该是必需的。
  • 对不起。这次是我的错。(我讨厌 VS 需要构建来更新语法高亮)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-11-08
相关资源
最近更新 更多