选定的TreeViewItem为null [英] Selected TreeViewItem is null

查看:108
本文介绍了选定的TreeViewItem为null的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想为TreeView创建一个ContextMenu。 TreeView XAML:

I wanted to create a ContextMenu for my TreeView. TreeView XAML:

<helper:ExtendedTreeView Grid.Row="5" ItemsSource="{Binding OCFrage, Mode=TwoWay}" SelectedItem_="{Binding SelectedItem, Mode=TwoWay}" SelectedItemChanged="treeView1_SelectedItemChanged" x:Name="treeView1" Height="205" Width="215">
    <helper:ExtendedTreeView.Resources>
        <HierarchicalDataTemplate DataType="{x:Type model:T_Frage}" ItemsSource="{Binding Wertung, Mode=TwoWay}">
            <TextBlock Text="{Binding Text}"/>
        </HierarchicalDataTemplate>
    </helper:ExtendedTreeView.Resources>
</helper:ExtendedTreeView>

而helper:ExtendedTreeView是此类:

while helper:ExtendedTreeView is this class:

public class ExtendedTreeView : TreeView
{
    public ExtendedTreeView()
        : base()
    {
        this.SelectedItemChanged += new RoutedPropertyChangedEventHandler<object>(self);
    }

    void self(object sender, RoutedPropertyChangedEventArgs<object> e)
    {
        if (SelectedItem != null)
        {
            SetValue(SelectedItem_Property, SelectedItem);
        }
    }

    public object SelectedItem_
    {
        get { return (object)GetValue(SelectedItem_Property); }
        set { SetValue(SelectedItem_Property, value); }
    }
    public static readonly DependencyProperty SelectedItem_Property = DependencyProperty.Register("SelectedItem_", typeof(object), typeof(ExtendedTreeView), new UIPropertyMetadata(null));
}

我正在使用它来绑定SelectedItem,并在ViewModel中使用它。

I'm using this for binding the SelectedItem and use it in my ViewModel.

我的TreeView具有标头,其类型为T_Frage,其节点的类型为T_Wertung,所以应该是

My TreeView has "Headers" which are of type T_Frage and their nodes are of type T_Wertung, so it would be


  • T_Frage

    • T_Wertung

    • T_Wertung


    • T_Wertung
      ...

    我想为Headers创建一个ContextMenu。因此,如果用户单击类型为T_Frage的TreeViewItem,则应弹出ContextMenu。我已按照本指南 http://canhandre.wordpress进行操作。 com / 2012/01/14 / wpf-treeview-with-contextmenu / ,而我目前正在CodeBehind中进行测试。问题是执行此代码时: TreeViewItem selectedItem = treeView1.SelectedItem as TreeViewItem; treeView1.SelectedItem类型为T_Frage并具有它的值,但是当我分配给selectedItem,selectedItem仍然为null ...为什么?

    I wanted to create a ContextMenu for the Headers. So if the user clicks on the TreeViewItem of type T_Frage, a ContextMenu should popup. I've followed this guide http://canhandre.wordpress.com/2012/01/14/wpf-treeview-with-contextmenu/ and I'm doing it currently in the CodeBehind just to test it. The problem is that when this code is being executed: TreeViewItem selectedItem = treeView1.SelectedItem as TreeViewItem; treeView1.SelectedItem is of type T_Frage and has it's values, but when I assign to selectedItem, selectedItem is still null... Why?

    编辑:在XAML中删除ItemsSource并创建普通的TreeViewItems

    Removing the ItemsSource and creating normal TreeViewItems in XAML

      <TreeViewItem Header="Edit" Name="Edit">
           <TreeViewItem Header="Text"/>
           <TreeViewItem Header="Image"/>
           <TreeViewItem Header="Table"/>
      </TreeViewItem>
    

    将在此处分配值 TreeViewItem selectedItem = treeView1.SelectedItem作为TreeViewItem; 。这意味着我无法在TreeView中将T_Frage类型的项目分配给TreeViewItem类型的变量。我应该如何为TreeView中非TreeViewItem类型的项目创建ContextMenu?就像您在上面看到的那样,将T_Frage的类型分配给TreeViewItem的结果将为null。

    will assign the value here TreeViewItem selectedItem = treeView1.SelectedItem as TreeViewItem;. This means I can't assign a Item in my TreeView, which is of type T_Frage, to a variable of type TreeViewItem. How am I supposed to create a ContextMenu for a Item in my TreeView which is not of type TreeViewItem? Like you can see above, assigning the type of T_Frage to TreeViewItem will result as null.

    推荐答案

    我知道现在是MVVM,不是CodeBehind,而是我想先在CodeBehind中对其进行测试,然后再在MVVM中进行所有操作,因为我认为立即在MVVM中进行操作会更困难...无论如何,这就是我想要的:

    I know that it's MVVM now and not CodeBehind, but I just wanted to test it in CodeBehind first and then do it all in MVVM, because I thought doing it right away in MVVM would be harder... Anyway, this is what I wanted:

    <helper:ExtendedTreeView Grid.Row="5" ItemsSource="{Binding OCFrage, Mode=TwoWay}" SelectedItem_="{Binding SelectedItem, Mode=TwoWay}" SelectedItemChanged="treeView1_SelectedItemChanged" x:Name="treeView1" Height="205" Width="215">
        <TreeView.ContextMenu>
            <ContextMenu ItemsSource="{Binding OCContext}"/>
        </TreeView.ContextMenu)
        <helper:ExtendedTreeView.Resources>
            <HierarchicalDataTemplate DataType="{x:Type model:T_Frage}" ItemsSource="{Binding Wertung, Mode=TwoWay}">
                <TextBlock Text="{Binding Text}"/>
            </HierarchicalDataTemplate>
        </helper:ExtendedTreeView.Resources>
    </helper:ExtendedTreeView>
    

    ViewModel:

    ViewModel:

    private object _selecteditem;
    public object SelectedItem
    {
        get { return _selecteditem; }
        set
        {
            OCContext = new ObservableCollection<T_Wertung>();
            if (value is T_Frage)
            {
                T_Frage selected = (T_Frage)value;
        //do something with selected
        OCContext.Add(new T_Wertung(1,"Test",100));
                }
            }
            RaisePropertyChanged(() => SelectedItem);
        }
    }
    
    private ObservableCollection<T_Wertung> _occontext;
    public ObservableCollection<T_Wertung> OCContext
    {
        get
        {
            if (_occontext == null)
                _occontext = new ObservableCollection<T_Wertung>();
            return _occontext;
        }
        set
        {
            _occontext = value;
            RaisePropertyChanged(() => OCContext);
        }
    }
    

    因为我绑定到TreeView的SelectedItem ,请问SelectedItem是否为T_Frage类型。如果是真的,我将创建一个T_Frage类型的新变量,并将其上下文设置为SelectedItem的值。现在,我可以对此项目进行处理,然后将一个项目添加到名为 OCContext 的ContextMenu列表中。当我右键单击该项目时,将弹出ContextMenu,它显示 OCContext 的项目。

    Since I'm binding to the SelectedItem of the TreeView, I can ask if the SelectedItem is of type T_Frage. If it's true, I'll create a new variable of type T_Frage and set it's context to the value of the SelectedItem. Now I can do something with this item and then add an item to the List of the ContextMenu named OCContext. When I right click the item, the ContextMenu is being popped up and it shows the items of OCContext.

    这篇关于选定的TreeViewItem为null的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆