选定的TreeViewItem为null

时间:2013-07-11 07:50:07

标签: c# wpf treeview contextmenu selecteditem

我想为TreeView创建一个ContextMenu。 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>

while helper:ExtendedTreeView就是这个类:

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中使用它。

我的TreeView有“标题”,类型为T_Frage,其节点类型为T_Wertung,因此它将是

  • T_Frage
    • T_Wertung
    • T_Wertung
  • T_Frage
    • 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 ...为什么?

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

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

将在此处TreeViewItem selectedItem = treeView1.SelectedItem as TreeViewItem;指定值。这意味着我无法将TreeView中的Item(类型为T_Frage)分配给TreeViewItem类型的变量。我应该如何为TreeView中的Item创建一个ContextMenu,它不是TreeViewItem类型的?您可以在上面看到,将T_Frage的类型分配给TreeViewItem将导致为null。

1 个答案:

答案 0 :(得分:0)

我知道它现在是MVVM而不是CodeBehind,但我只想先在CodeBehind中测试它,然后在MVVM中完成所有操作,因为我认为在MVVM中立即执行它会更难...无论如何,这就是我想要的:

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

视图模型:

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的项目。