|
<!--
// (c) Copyright Microsoft Corporation.
// This source is subject to the Microsoft Public License (Ms-PL).
// Please see http://go.microsoft.com/fwlink/?LinkID=131993 for details.
// All other rights reserved.
-->
<UserControl
xmlns="http://schemas.microsoft.com/client/2007"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:controls="clr-namespace:Microsoft.Windows.Controls;assembly=Microsoft.Windows.Controls"
xmlns:common="clr-namespace:Microsoft.Windows.Controls.Samples;assembly=Microsoft.Windows.Controls.Samples.Common"
x:Class="Microsoft.Windows.Controls.Samples.TreeViewSelectionSample">
<StackPanel>
<!-- TreeView and TreeViewItem Events -->
<ContentControl Content="SelectedItem and SelectedValue" Style="{StaticResource Header}" />
<StackPanel>
<TextBlock Text="TreeView.SelectedItem" Style="{StaticResource ApiName}" />
<TextBlock Style="{StaticResource ApiDescription}">
The selected object in the TreeView, or null if no item is
selected.
</TextBlock>
<TextBlock Text="TreeView.SelectedValuePath" Style="{StaticResource ApiName}" />
<TextBlock Style="{StaticResource ApiDescription}">
A string that contains the path that is used to get the
SelectedValue.
</TextBlock>
<TextBlock Text="TreeView.SelectedValue" Style="{StaticResource ApiName}" />
<TextBlock Style="{StaticResource ApiDescription}">
Gets the object that is at the specified SelectedValuePath of
the SelectedItem, or null if no item is selected.
</TextBlock>
</StackPanel>
<!-- Selection example -->
<ContentControl Content="Selection" Style="{StaticResource Header}" />
<TextBlock Style="{StaticResource Information}">
Change the selection to see the SelectedItem and SelectedValue.
</TextBlock>
<controls:TreeView
x:Name="TreeOfLife"
SelectedItemChanged="OnSelectedItemChanged"
SelectedValuePath="Classification"
Margin="5">
<controls:TreeView.ItemTemplate>
<controls:HierarchicalDataTemplate ItemsSource="{Binding Subclasses}">
<StackPanel>
<TextBlock Text="{Binding Rank}" FontSize="8" FontStyle="Italic" Foreground="Gray" Margin="0 0 0 -5" />
<TextBlock Text="{Binding Classification}" />
</StackPanel>
</controls:HierarchicalDataTemplate>
</controls:TreeView.ItemTemplate>
</controls:TreeView>
<Grid HorizontalAlignment="Left">
<Grid.RowDefinitions>
<RowDefinition />
<RowDefinition />
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition />
<ColumnDefinition />
</Grid.ColumnDefinitions>
<TextBlock Text="SelectedItem:" Style="{StaticResource Information}" VerticalAlignment="Center" />
<ContentControl x:Name="SelectedItem" FontWeight="Bold" Grid.Column="1" VerticalAlignment="Center" />
<TextBlock Grid.Row="1" Text="SelectedValue:" Style="{StaticResource Information}" VerticalAlignment="Center" />
<ContentControl x:Name="SelectedValue" FontWeight="Bold" Grid.Row="1" Grid.Column="1" VerticalAlignment="Center" />
</Grid>
<src:SourceViewer xmlns:src="clr-namespace:Microsoft.Windows.Controls.Samples;assembly=Microsoft.Windows.Controls.Samples.Common" xmlns:sys="clr-namespace:System;assembly=mscorlib">
<src:SourceFile Path="TreeViewSelectionSample.xaml">
<src:SourceFile.Source>
<sys:String><!--
// (c) Copyright Microsoft Corporation.
// This source is subject to the Microsoft Public License (Ms-PL).
// Please see http://go.microsoft.com/fwlink/?LinkID=131993 for details.
// All other rights reserved.
-->
<UserControl
xmlns="http://schemas.microsoft.com/client/2007"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:controls="clr-namespace:Microsoft.Windows.Controls;assembly=Microsoft.Windows.Controls"
xmlns:common="clr-namespace:Microsoft.Windows.Controls.Samples;assembly=Microsoft.Windows.Controls.Samples.Common"
x:Class="Microsoft.Windows.Controls.Samples.TreeViewSelectionSample">
<StackPanel>
<!-- TreeView and TreeViewItem Events -->
<ContentControl Content="SelectedItem and SelectedValue" Style="{StaticResource Header}" />
<StackPanel>
<TextBlock Text="TreeView.SelectedItem" Style="{StaticResource ApiName}" />
<TextBlock Style="{StaticResource ApiDescription}">
The selected object in the TreeView, or null if no item is
selected.
</TextBlock>
<TextBlock Text="TreeView.SelectedValuePath" Style="{StaticResource ApiName}" />
<TextBlock Style="{StaticResource ApiDescription}">
A string that contains the path that is used to get the
SelectedValue.
</TextBlock>
<TextBlock Text="TreeView.SelectedValue" Style="{StaticResource ApiName}" />
<TextBlock Style="{StaticResource ApiDescription}">
Gets the object that is at the specified SelectedValuePath of
the SelectedItem, or null if no item is selected.
</TextBlock>
</StackPanel>
<!-- Selection example -->
<ContentControl Content="Selection" Style="{StaticResource Header}" />
<TextBlock Style="{StaticResource Information}">
Change the selection to see the SelectedItem and SelectedValue.
</TextBlock>
<controls:TreeView
x:Name="TreeOfLife"
SelectedItemChanged="OnSelectedItemChanged"
SelectedValuePath="Classification"
Margin="5">
<controls:TreeView.ItemTemplate>
<controls:HierarchicalDataTemplate ItemsSource="{Binding Subclasses}">
<StackPanel>
<TextBlock Text="{Binding Rank}" FontSize="8" FontStyle="Italic" Foreground="Gray" Margin="0 0 0 -5" />
<TextBlock Text="{Binding Classification}" />
</StackPanel>
</controls:HierarchicalDataTemplate>
</controls:TreeView.ItemTemplate>
</controls:TreeView>
<Grid HorizontalAlignment="Left">
<Grid.RowDefinitions>
<RowDefinition />
<RowDefinition />
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition />
<ColumnDefinition />
</Grid.ColumnDefinitions>
<TextBlock Text="SelectedItem:" Style="{StaticResource Information}" VerticalAlignment="Center" />
<ContentControl x:Name="SelectedItem" FontWeight="Bold" Grid.Column="1" VerticalAlignment="Center" />
<TextBlock Grid.Row="1" Text="SelectedValue:" Style="{StaticResource Information}" VerticalAlignment="Center" />
<ContentControl x:Name="SelectedValue" FontWeight="Bold" Grid.Row="1" Grid.Column="1" VerticalAlignment="Center" />
</Grid>
</StackPanel>
</UserControl></sys:String>
</src:SourceFile.Source>
</src:SourceFile>
<src:SourceFile Path="TreeViewSelectionSample.xaml.cs">
<src:SourceFile.Source>
<sys:String>// (c) Copyright Microsoft Corporation.
// This source is subject to the Microsoft Public License (Ms-PL).
// Please see http://go.microsoft.com/fwlink/?LinkID=131993 for details.
// All other rights reserved.
using System;
using System.Diagnostics.CodeAnalysis;
using System.Globalization;
using System.Windows;
using System.Windows.Controls;
using System.ComponentModel;
namespace Microsoft.Windows.Controls.Samples
{
/// <summary>
/// Sample demonstrating TreeView selection.
/// </summary>
[Sample("(2)Selection", DifficultyLevel.Basic)]
[Category("TreeView")]
public partial class TreeViewSelectionSample : UserControl
{
/// <summary>
/// Initializes a new instance of the TreeViewSelectionSample class.
/// </summary>
public TreeViewSelectionSample()
{
InitializeComponent();
// Fill the tree with data
TreeOfLife.ItemsSource = Taxonomy.Life;
}
/// <summary>
/// Handle the TreeView.SelectedItemChanged event.
/// </summary>
/// <param name="sender">The TreeView.</param>
/// <param name="e">Event arguments.</param>
[SuppressMessage("Microsoft.Performance", "CA1811:AvoidUncalledPrivateCode", Justification = "The event handler is declared in XAML.")]
private void OnSelectedItemChanged(object sender, RoutedPropertyChangedEventArgs<object> e)
{
TreeView view = sender as TreeView;
SelectedItem.Content = view.SelectedItem;
SelectedValue.Content = view.SelectedValue;
}
}
}</sys:String>
</src:SourceFile.Source>
</src:SourceFile>
</src:SourceViewer>
</StackPanel>
</UserControl>
|
By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.
If a file you wish to view isn't highlighted, and is a text file (not binary), please
let us know and we'll add colourisation support for it.
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.