0

On my Control is a TreeView with a set of Items which can be in one of three levels. The Objects inside the TreeView have a property called Level which determines this. Under the TreeView there are some TextBoxes. The TextBoxes should be filled depending on the level of the SelectedItem in the TreeView. If an Item on the top level is selected only the first TextBox should be filled. If an item on the second level is selected the first an second TextBox should be filled and so on. This is the structure of items in the TreeView:

 -Item1                  <- Level = 0
 -- SubItem1             <- Level = 1
  -- SubSubItem1         <- Level = 2
  -- SubSubItem2
 -Item2                  <- Level = 0
 -- SubItem1             <- Level = 1
  -- SubSubItem1         <- Level = 2
 -- SubItem2
  -- SubSubItem1

I have tried to acomplish this task with a trigger in both, the HierarchicalDataTemplate of the TreeView and the style of the TextBox. The problem with the HierarchicalDataTemplate of the TreeView is, that I have no access on the TextBox since it (the TextBox) is not in the scope of the DataTemplate and does not compile. The other attempt simply doesn't produce a result. This is the XAML of my control:

<TreeView Grid.Row="1" Margin="5" ItemsSource="{Binding Privileges}" Name="Privileges">
     <TreeView.ItemTemplate>
          <HierarchicalDataTemplate ItemsSource="{Binding Items}">
              <TextBlock Text="{Binding}"/>
           </HierarchicalDataTemplate>
     </TreeView.ItemTemplate>
</TreeView>
<TextBox Grid.Row="0" Margin="3">
     <TextBox.Style>
          <Style>
              <Style.Triggers>
                   <DataTrigger Binding="{Binding Path=Privileges.SelectedItem.Level}" Value="0">
                         <Setter Property="TextBox.Text" Value="Test"/>
                   </DataTrigger>
               </Style.Triggers>
          </Style>
      </TextBox.Style>
 </TextBox>
Romano Zumbé
  • 7,893
  • 4
  • 33
  • 55

1 Answers1

0

You cannot get the level directly (as explained here). But you can adapt one of the functions in the aforementioned link and calculate it:

    private void treeView1_SelectedItemChanged(object sender, RoutedPropertyChangedEventArgs<object> e)
    {
        int level = FindTreeLevel((TreeViewItem)treeView1.SelectedItem);
        if (level == 0)
        {
            textBox1.Text = "Item1";
        }
        else if (level == 1)
        {
            textBox1.Text = "Item1";
            textBox2.Text = "SubItem1";
        }
        //etc.
    }

    private int FindTreeLevel(TreeViewItem control)
    {
        var level = 0;
        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;
    }
Community
  • 1
  • 1
varocarbas
  • 12,354
  • 4
  • 26
  • 37
  • I already have the level as a property of the TreeeViewItem. It is set while populating it. My question was how I could set the TextBox Text depending on this properties Value. I think Level may be a little confusing as a Property name in this context. Do you have any Idea? – Romano Zumbé Jun 27 '13 at 13:59
  • And it should not work from code since I want to use a strict MVVM approach – Romano Zumbé Jun 27 '13 at 14:01
  • No please leave it. Could clear things up for somebody else who could help me. Thanks :-) – Romano Zumbé Jun 27 '13 at 14:06
  • OK. I will leave it then. Good luck. – varocarbas Jun 27 '13 at 14:07