[英]Selected TreeViewItem is null
I wanted to create a ContextMenu for my TreeView. 我想为我的TreeView创建一个ContextMenu。 TreeView XAML:
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 is this class: 而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));
}
I'm using this for binding the SelectedItem and use it in my ViewModel. 我正在使用它来绑定SelectedItem,并在ViewModel中使用它。
My TreeView has "Headers" which are of type T_Frage and their nodes are of type T_Wertung, so it would be 我的TreeView具有“标头”,其类型为T_Frage,其节点的类型为T_Wertung,因此
I wanted to create a ContextMenu for the Headers. 我想为页眉创建一个ContextMenu。 So if the user clicks on the TreeViewItem of type T_Frage, a ContextMenu should popup.
因此,如果用户单击T_Frage类型的TreeViewItem,则应弹出ContextMenu。 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.
我已经按照本指南http://canhandre.wordpress.com/2012/01/14/wpf-treeview-with-contextmenu/进行了操作,目前正在CodeBehind中进行测试。 The problem is that when this code is being executed:
TreeViewItem selectedItem = treeView1.SelectedItem as TreeViewItem;
问题是执行此代码时:
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? treeView1.SelectedItem类型为T_Frage,具有它的值,但是当我分配给selectedItem时,selectedItem仍为null ...为什么?
Edit: Removing the ItemsSource and creating normal TreeViewItems in XAML 编辑:删除ItemsSource并在XAML中创建普通的TreeViewItems
<TreeViewItem Header="Edit" Name="Edit">
<TreeViewItem Header="Text"/>
<TreeViewItem Header="Image"/>
<TreeViewItem Header="Table"/>
</TreeViewItem>
will assign the value here TreeViewItem selectedItem = treeView1.SelectedItem as TreeViewItem;
将在此处分配值
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.
这意味着我不能将T_Frage类型的TreeView中的项分配给TreeViewItem类型的变量。 How am I supposed to create a ContextMenu for a Item in my TreeView which is not of type TreeViewItem?
我应该如何为TreeView中非TreeViewItem类型的项目创建ContextMenu? Like you can see above, assigning the type of T_Frage to TreeViewItem will result as null.
如上所示,将T_Frage的类型分配给TreeViewItem的结果将为null。
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: 我知道现在是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>
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);
}
}
Since I'm binding to the SelectedItem of the TreeView, I can ask if the SelectedItem is of type T_Frage. 由于我绑定到TreeView的SelectedItem,因此我可以询问SelectedItem是否为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.
如果是真的,我将创建一个T_Frage类型的新变量,并将其上下文设置为SelectedItem的值。 Now I can do something with this item and then add an item to the List of the ContextMenu named
OCContext
. 现在,我可以对此项目进行处理,然后将一个项目添加到名为
OCContext
的ContextMenu的列表中。 When I right click the item, the ContextMenu is being popped up and it shows the items of OCContext
. 当我右键单击该项目时,将弹出ContextMenu并显示
OCContext
的项目。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.