Mvvm Light TreeView 数据绑定
public class TreeNode
{
public string NodeID { get; set; }
public string NodeName { get; set; }
public List<TreeNode> Children { get; set; }
}
<UserControl x:Class="TreeViewDemo.MainPage3" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:sdk="http://schemas.microsoft.com/winfx/2006/xaml/presentation/sdk" mc:Ignorable="d" d:DesignHeight="300" d:DesignWidth="400">
<Grid x:Name="LayoutRoot" Background="White"> <Grid.Resources> <sdk:HierarchicalDataTemplate x:Key="ChildTemplate" ItemsSource="{Binding Path=Children}" > <TextBlock FontStyle="Italic" Text="{Binding Path=NodeName}" /> </sdk:HierarchicalDataTemplate> <sdk:HierarchicalDataTemplate x:Key="RootTemplate" ItemsSource="{Binding Path=Children}" ItemTemplate="{StaticResource ChildTemplate}"> <TextBlock Text="{Binding Path=NodeName}" FontWeight="Bold" /> </sdk:HierarchicalDataTemplate> </Grid.Resources> <sdk:TreeView Height="200" HorizontalAlignment="Left" Margin="12,12,0,0" VerticalAlignment="Top" Width="120" ItemsSource="{Binding}" ItemTemplate="{StaticResource RootTemplate}" x:Name="myTreeView"/> </Grid> </UserControl>
using System; using System.Collections.Generic; using System.Linq; using System.Net; using System.Windows; using System.Windows.Controls; using System.Windows.Documents; using System.Windows.Input; using System.Windows.Media; using System.Windows.Media.Animation; using System.Windows.Shapes; using System.Collections.ObjectModel; namespace TreeViewDemo {
public partial class MainPage3 : UserControl { // Create the topics collection. static public ObservableCollection<TreeNode> Topics = new ObservableCollection<TreeNode>(); public MainPage3() { InitializeComponent();
// Add some topics to the collection. TreeNode One = new TreeNode() { NodeName = "1" }; One.Children = new ObservableCollection<TreeNode>(); One.Children.Add(new TreeNode { NodeName = "1-1"}); Topics.Add(One); myTreeView.DataContext = Topics; }
void myTreeView_MouseEnter(object sender, MouseEventArgs e) { ExpandAll(sender, e); }
// Method to expand all the treeview children. private void ExpandAll(object sender, RoutedEventArgs e) { for (int i = 0; i < myTreeView.Items.Count; i++) { TreeViewItem childItem = myTreeView.ItemContainerGenerator.ContainerFromItem(myTreeView.Items[i]) as TreeViewItem; if (childItem != null) ExpandAllTreeViewItems(childItem); } }
// Declare a delegate for the method to expand all children. You'll do this // asynchronously to ensure the items are instatiated before they are expanded. public delegate void ExpandChildren(TreeViewItem currentTreeViewItem);
// Exapand all the children of the current treeview item. private void ExpandAllTreeViewItems(TreeViewItem currentTreeViewItem) { // If the current node is not expanded, call the expansion asynchronously // to ensure the items are instantiated before expansion. if (!currentTreeViewItem.IsExpanded) { currentTreeViewItem.IsExpanded = true;
// Asynchronous call to expand the children. currentTreeViewItem.Dispatcher.BeginInvoke(new ExpandChildren(ExpandAllTreeViewItems), new object[] { currentTreeViewItem });
// Alternatively, you could make the call to ExpandAllTreeViewItems // using lambda syntax. // currentTreeViewItem.Dispatcher.BeginInvoke(() => // ExpandAllTreeViewItems(currentTreeViewItem)); }
else { for (int i = 0; i < currentTreeViewItem.Items.Count; i++) { TreeViewItem child = (TreeViewItem) currentTreeViewItem.ItemContainerGenerator.ContainerFromIndex(i); ExpandAllTreeViewItems(child); } } }
// Simple business object. } }