您可以實現類似的結合UserControl
與一些ItemsControls
。
這裏是我使用的模型:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace TreeViewSample
{
public class Comment
{
public string Text { get; set; }
public IEnumerable<Comment> Comments { get; set; }
}
public class MainViewModel
{
public IEnumerable<Comment> Comments { get; set; }
public MainViewModel()
{
Comments = new[]
{
new Comment
{
Text = "root comment #1",
Comments = new[]
{
new Comment
{
Text = "comment #1 #1",
Comments = new[]
{
new Comment
{
Text = "comment #1 #1 #1"
}
}
},
new Comment
{
Text = "comment #1 #2"
}
}
},
new Comment
{
Text = "root comment #2",
Comments = new[]
{
new Comment
{
Text = "comment #2 #1",
}
}
}
};
}
}
}
這裏是UserControl
:
XAML:
<UserControl
x:Class="TreeViewSample.CommentView"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:TreeViewSample"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d"
d:DesignHeight="300"
d:DesignWidth="400">
<Grid x:Name="root" Margin="10,10,0,0">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto"></ColumnDefinition>
<ColumnDefinition></ColumnDefinition>
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition Height="Auto"></RowDefinition>
<RowDefinition></RowDefinition>
</Grid.RowDefinitions>
<StackPanel Background="Gray">
<AppBarButton Icon="Upload" IsCompact="True"></AppBarButton>
<AppBarButton Icon="Download" IsCompact="True"></AppBarButton>
</StackPanel>
<Border Background="Gray" VerticalAlignment="Stretch" Grid.Column="1">
<TextBlock FontSize="30" VerticalAlignment="Center" Text="{Binding Comment.Text}"></TextBlock>
</Border>
<ItemsControl Grid.Column="1" Grid.Row="1" ItemsSource="{Binding Comment.Comments}">
<ItemsControl.ItemTemplate>
<DataTemplate>
<local:CommentView Comment="{Binding}"></local:CommentView>
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
</Grid>
</UserControl>
後面的代碼:
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
namespace TreeViewSample
{
public sealed partial class CommentView : UserControl
{
public Comment Comment
{
get { return (Comment)GetValue(CommentProperty); }
set { SetValue(CommentProperty, value); }
}
public static readonly DependencyProperty CommentProperty = DependencyProperty.Register("Comment", typeof(Comment), typeof(CommentView), new PropertyMetadata(null));
public CommentView()
{
this.InitializeComponent();
root.DataContext = this;
}
}
}
而且主頁:
XAML:
<Page
x:Class="TreeViewSample.MainPage"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:TreeViewSample"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d">
<Page.DataContext>
<local:MainViewModel></local:MainViewModel>
</Page.DataContext>
<Grid Margin="0,100,0,0" Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">
<ItemsControl ItemsSource="{Binding Comments}">
<ItemsControl.ItemTemplate>
<DataTemplate>
<local:CommentView Comment="{Binding}"></local:CommentView>
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
</Grid>
</Page>
如果重用這種觀點很多時候你可以分享這個DataTemplate
或定義專用的「CommentsView」 UserControl
了。
後面的代碼:
using Windows.UI.Xaml.Controls;
namespace TreeViewSample
{
public sealed partial class MainPage : Page
{
public MainPage()
{
this.InitializeComponent();
}
}
}
結果:
注意,有在WinRT XAML Toolkit一個TreeView
執行(你可以直接從的Visual Studio與安裝的NuGet )