开发者

Which item was selected in my TreeView

In my application I build a TreeView by a binding. The ItemSource contains a list of items of type X.

X has a property 'Properties' some of these properties are references to an item in the parent list.

The TreeView starts at a selected root (an item from the list). All properties which are references are added to this root as TreeViewItems. However, they can be expanded too until the end is reached, or not; I mean that this could theoretically be an infinite recursion for it is possible that a reference refers to itself.

The question now is, how do I know which Item is selected?

I need to know the level and all its parents.

I thought up these things:

  • While expanding the tree, keep track of the levels and parents.

    This does not work obviously because after selecting an other item below a different parent, I lose track of the other path.

  • Check for the e.OldValue. This also doesn't work for the same reason of the previous try.
  • While expanding (and with that creating) the TreeView, construct a TreeView like variable in c# behind the code.

    At first this seems to be quite close to the solution since you cannot remove nodes from the TreeView ones they are created. For example: You go to the 5th level and collapsed the first level. If you expand the first level you'll see the the path exacly the same as before the collapse.

To create the TreeView in this way I had to override the DataTemplateSelector. (C#)

public class TreeViewDataTemplateSelector : DataTemplateSelector
{
    public override DataTemplate SelectTemplate(object item, DependencyObject container)
    {
        FrameworkElement element = container as FrameworkElement;
        if (element != null && item != null)
        {
    开发者_运维百科        if (item is Entity)
                return element.FindResource("XTemplate") as DataTemplate;
            else
                return element.FindResource("PropertyWithReferenceTemplate") as DataTemplate;
        }
        return null;
    }
}

The Binding and HierarachialDataTemplate. (Xaml)

<TreeView ItemsSource="{Binding ElementName=view, Path=XDefinition}" ItemTemplateSelector="{StaticResource treeViewDataTemplateSelector}"  SelectedItemChanged="TreeView_SelectedItemChanged">
    <TreeView.Resources>
        <HierarchicalDataTemplate x:Key="XTemplate" ItemsSource="{Binding Path=Properties}">
            <TextBlock Text="{Binding Path=Name}" />                            
        </HierarchicalDataTemplate>
        <HierarchicalDataTemplate x:Key="PropertyWithReferenceTemplate" ItemsSource="{Binding Path=ReferenceToX.Properties,}">
            <TextBlock Text="{Binding Path=Name}" />
        </HierarchicalDataTemplate>
    </TreeView.Resources>
</TreeView>

Can anybody help me on this problem?

If there is anything else you need to know please tell me and I'll update my question.

Solution I had to change my event SelectedItemChanged="TreeView_SelectedItemChanged" to TreeViewItem.Selected="TreeViewItem_Selected". With the help of the LogicalTreeHelper.GetParent i was able to retrieve the correct parent.


What about looking up the VisualTree? You can go up until you reach the top item. Before you can evaluate all single items.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜