如何捕获树视图项目? [英] How do I catch treeview item extended?

查看:97
本文介绍了如何捕获树视图项目?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好,



我正在使用WPF树视图。我的项目位于分层数据模板中。

我想使用MVVM捕获扩展项事件,并在视图模型上向函数发送在它所代表的对象上扩展的树视图。



先谢谢,



这是我的代码:



Hello,

I am using WPF treeview. My Items are in Hierarchical data template.
I want to catch the extended item event, using MVVM, and send to the function on view model the treeview that was extended on the object it represents.

Thanks ahead,

Here is my code:

<TreeView  Name="ScenariosTreeView" ItemsSource="{Binding Path=Cat, Mode=TwoWay}" Focusable="True" >

               <TreeView.InputBindings>
                   <KeyBinding Key="Delete" Command="{Binding DeleteCommand}" CommandParameter="{Binding SelectedValue ,ElementName=ScenariosTreeView}" />
               </TreeView.InputBindings>


               <TreeView.ItemContainerStyle>
                   <Style TargetType="{x:Type TreeViewItem}">
                       <Setter Property="IsExpanded" Value="{Binding IsExtended, Mode=TwoWay}" />
                       <Setter Property="IsSelected" Value="{Binding IsExtended, Mode=TwoWay}"/>
                   </Style>
               </TreeView.ItemContainerStyle>

               <TreeView.Resources>

                       <HierarchicalDataTemplate DataType="{x:Type sotc:ScenarioCategory}" ItemsSource="{Binding Path=ScenarioList}" >
                       <TextBlock  Text="{Binding Path=Name}" Foreground="Black" IsEnabled="True" Focusable="True"/>
                      </HierarchicalDataTemplate>


                   <DataTemplate DataType="{x:Type sotc:Scenario}" >
                       <TextBlock  Text="{Binding Path=Name, Mode=TwoWay}" />
                   </DataTemplate>

               </TreeView.Resources>

           </TreeView>







IsExpanded事件确实如此没有激动,任何想法?




The IsExpanded event does not fires up, any ideas?

推荐答案

请看我对这个问题的评论。如果你的意思是节点展开时的事件,这是 TreeViewItem 的事件:

TreeViewItem.Expanded Event(System.Windows.Controls) [ ^ ]。



代码示例显示了将事件处理程序手动添加到调用列表的情况XAML中的事件实例。例如,您可能必须最终遍历树并将事件处理程序添加到每个节点。



-SA
Please see my comment to the question. If you mean the event when a node is expanded, this is the event of TreeViewItem:
TreeViewItem.Expanded Event (System.Windows.Controls)[^].

The code sample shows the case when an event handler is "manually" added to the invocation list of the event instance, in XAML. It's possible that you have to, for example, eventually traverse the tree and add the event handler to each node.

—SA


无需弄乱控件的事件 - 只需将 IsExpanded 属性绑定到视图模型上的属性,并拥有视图-model在属性更改时执行任何操作。



查看本文以更深入地了解如何将TreeView控件与MVVM一起使用:

使用简化WPF TreeView ViewModel模式 [ ^ ]
No need to mess around with the control's events - just bind the IsExpanded property to a property on your view-model, and have the view-model do whatever you need it to when the property changes.

Have a look at this article for a more in-depth look at using the TreeView control with MVVM:
Simplifying the WPF TreeView by Using the ViewModel Pattern[^]


如果您只是希望它们展开或折叠,那就是您正在寻找的所有内容,并且您在ObservableCollection中拥有它们,那么onchanged事件只会触发Add(),Clear()和Remove()事件,所以请尝试使用templist来保存你的itemsource



If you just want them to expand or collapse and that is all you are looking for and you have them in the ObservableCollection, then the onchanged event will only fire on the Add(), Clear(), and Remove() events, so try a templist to hold your itemsource

foreach(type item in Cat)
{
item.IsExpanded = true;
templist.add(item);
}
Cat.Clear();
foreach(type item in templist)
{
Cat.Add(item);
}





可观察收藏品中的一个有趣问题



one of those funny issues with observable collections


这篇关于如何捕获树视图项目?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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