1
code example這顯示瞭如何得到滾動條的位置在的ScrollViewer(與ScrollToVerticalOffset),使得例如如果需要重新創建,可以將滾動條重置到此位置。有沒有辦法獲取TreeView節點的展開/摺疊狀態?
有什麼辦法可以爲TreeView控件執行此操作,即獲取用戶擴展的節點索引集合?
code example這顯示瞭如何得到滾動條的位置在的ScrollViewer(與ScrollToVerticalOffset),使得例如如果需要重新創建,可以將滾動條重置到此位置。有沒有辦法獲取TreeView節點的展開/摺疊狀態?
有什麼辦法可以爲TreeView控件執行此操作,即獲取用戶擴展的節點索引集合?
最簡單的方法是將TreeViewItem.IsExpanded屬性綁定到ViewModel,然後通過模型和計算。
我給你寫了一個例子。它計算擴展節點的數量,但你可以做你想要擴大傢伙什麼...
C#:
using System;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Windows;
namespace WpfApplication1
{
public partial class TestBrowser : Window
{
private TreeViewItemViewModel[] _items;
public TestBrowser()
{
InitializeComponent();
var item1 = new TreeViewItemViewModel();
var item2 = new TreeViewItemViewModel();
var item3 = new TreeViewItemViewModel();
item3.Children.Add(new TreeViewItemViewModel());
item3.Children.Add(new TreeViewItemViewModel());
var child3 = new TreeViewItemViewModel();
child3.Children.Add(new TreeViewItemViewModel());
item3.Children.Add(child3);
_items = new[] {item1, item2, item3};
tv.DataContext = _items;
}
private void CalculateExpandedClick(object sender, RoutedEventArgs e)
{
var expanded = 0;
foreach (TreeViewItemViewModel item in _items)
{
expanded += GetNumberOfExpanded(item);
}
ExpandedNumber.Text = expanded.ToString();
}
private int GetNumberOfExpanded(TreeViewItemViewModel model)
{
var expandedCount = 0;
if (model.IsExpanded)
{
expandedCount += 1;
foreach (TreeViewItemViewModel child in model.Children)
{
expandedCount += GetNumberOfExpanded(child);
}
}
return expandedCount;
}
}
/// <summary>
/// Single tree view item view model.
/// </summary>
public class TreeViewItemViewModel : INotifyPropertyChanged
{
public ObservableCollection<TreeViewItemViewModel> Children
{
get; private set;
}
private bool _isExpanded;
private string _text;
public bool IsExpanded
{
get { return _isExpanded; }
set
{
_isExpanded = value;
OnPropertyChanged("IsExpanded");
}
}
public string Text
{
get { return _text; }
set
{
_text = value;
OnPropertyChanged("Text");
}
}
public TreeViewItemViewModel()
{
Children = new ObservableCollection<TreeViewItemViewModel>();
Text = DateTime.Now.ToLongTimeString(); // Just fake data.
}
public event PropertyChangedEventHandler PropertyChanged;
private void OnPropertyChanged(string name)
{
var changed = PropertyChanged;
if (changed != null)
{
changed(this, new PropertyChangedEventArgs(name));
}
}
}
}
XAML:
<Window x:Class="WpfApplication1.TestBrowser"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="Expanded Test"
Height="300"
Width="300">
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*" />
<ColumnDefinition Width="*" />
</Grid.ColumnDefinitions>
<TreeView x:Name="tv"
ItemsSource="{Binding}">
<TreeView.ItemContainerStyle>
<Style TargetType="{x:Type TreeViewItem}">
<Setter Property="IsExpanded" Value="{Binding IsExpanded, Mode=TwoWay}" />
</Style>
</TreeView.ItemContainerStyle>
<TreeView.ItemTemplate>
<HierarchicalDataTemplate ItemsSource="{Binding Children}">
<TextBlock Text="{Binding Text}" />
</HierarchicalDataTemplate>
</TreeView.ItemTemplate>
</TreeView>
<StackPanel Grid.Column="1">
<Button Content="Get number of expanded items"
Click="CalculateExpandedClick"/>
<TextBlock x:Name="ExpandedNumber" />
</StackPanel>
</Grid>
</Window>
希望這有助於。
作爲一個WPF新手(大約3周),我不得不說這是真正的AWFUL,這是多麼難以發現。我一直在Google上搜索5個小時,可能從來沒有想過這是不是這個職位,一旦我終於想出了什麼谷歌。 – 2013-02-08 00:43:54