Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
189 views
in Technique[技术] by (71.8m points)

c# - the level of a treeview in WPF?

In a WinForms application, the level of a treeview is given by node.level
What is the corresponding command in WPF?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Given the Question:-

so when I click a node, how do I know which level it is? is there workaround?

Here's a possible workaround:-

If you have a reference to a Control in the Visual Tree, possibly from a Click event then you can use that control to work out which level it is in the tree view by calling a function like this, that I've used in the past.

private int FindTreeLevel(DependencyObject control)
{
  var level = -1;
  if (control != null)
  {
    var parent = VisualTreeHelper.GetParent(control);
    while (!(parent is TreeView) && (parent != null))
    {
      if (parent is TreeViewItem)
        level++;
      parent = VisualTreeHelper.GetParent(parent);
    }
  }
  return level;
}

This method will walk up the VisualTree and count how many TreeViewItem controls it finds before stopping when it finds the TreeView Control.

If you need this available in XAML, for example, a DataTrigger of a HierarchicalDataTemplate then you could package this up in to a IValueConverter class

<Window.Resources>
  <local:TreeLevelConverter x:Key="treeLevelConverter"/>

  <HierarchicalDataTemplate DataType="{x:Type local:MyType}" >
  ...
     <Grid ... >
         <TextBlock x:Name="MyControl" ... />
     ...
     </Grid>
     <HierarchicalDataTemplate.Triggers>
        <DataTrigger Binding="{Binding RelativeSource={RelativeSource Self}, Converter={StaticResource treeLevelConverter}}" Value="0" >
              <Setter TargetName="MyControl" Property="Background" Value="Red"/>
        </DataTrigger>
    </HierarchicalDataTemplate.Triggers>
  </HierarchicalDataTemplate>
</Window.Resources>

Then use the following Converter

  public class TreeLevelConverter : IValueConverter
  {
    public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
    {
      var level = -1;
      if (value is DependencyObject )
      {
        var parent = VisualTreeHelper.GetParent(value as DependencyObject );
        while (!(parent is TreeView) && (parent != null))
        {
          if (parent is TreeViewItem) 
            level++;
          parent = VisualTreeHelper.GetParent(parent);
        }
      }
      return level;
    }

    public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
    {
      throw new System.NotImplementedException();
    }
  }

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...