Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
135 changes: 112 additions & 23 deletions src/MainDemo.Wpf/Trees.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@
</Grid.ColumnDefinitions>

<Grid.RowDefinitions>
<RowDefinition Height="Auto" />
<RowDefinition Height="Auto" MaxHeight="450" />
<RowDefinition Height="Auto" />
<RowDefinition Height="Auto" MaxHeight="450" />
<RowDefinition Height="Auto" />
Expand Down Expand Up @@ -203,20 +205,22 @@
</Grid>
</smtx:XamlDisplay>

<TextBlock Style="{StaticResource MaterialDesignHeadline6TextBlock}" Text="Multi-Select Tree View:"
Grid.Column="2"/>
<smtx:XamlDisplay Grid.Row="1"
Grid.Column="2"
VerticalContentAlignment="Top"
UniqueKey="trees_3">
<TextBlock Style="{StaticResource MaterialDesignHeadline6TextBlock}" Text="Multi-Select Tree List View:"
Grid.Row="2"
Grid.Column="0"
Margin="0,40,0,0"/>
<smtx:XamlDisplay Grid.Row="3"
Grid.Column="0"
VerticalContentAlignment="Top"
UniqueKey="trees_3"
Margin="0,75,0,20">
<Grid>
<materialDesign:TreeListView MinWidth="220" MaxHeight="450"
ItemsSource="{Binding TreeItems}"
SelectedItem="{Binding SelectedTreeItem}">
<materialDesign:TreeListView.Resources>
<HierarchicalDataTemplate DataType="{x:Type domain:TestItem}"
ItemsSource="{Binding Items, Mode=OneTime}">
<TextBlock Margin="3,2" Text="{Binding Name, Mode=OneTime}" />
<HierarchicalDataTemplate DataType="{x:Type domain:TestItem}" ItemsSource="{Binding Items, Mode=OneTime}">
<TextBlock VerticalAlignment="Center" Text="{Binding Name, Mode=OneTime}" />
</HierarchicalDataTemplate>

<HierarchicalDataTemplate DataType="{x:Type domain:MovieCategory}"
Expand All @@ -231,19 +235,20 @@
</DataTemplate>
</materialDesign:TreeListView.Resources>

<!--
Because Data Virtualization is enabled on this tree view by default, if you don't bind the IsExpanded property to something in the bound view model,
you can loose the expanded state of items when the TreeListViewItem is recycled.

For more information:
https://github.com/MaterialDesignInXAML/MaterialDesignInXamlToolkit/issues/3640#issuecomment-2274086113
https://learn.microsoft.com/dotnet/desktop/wpf/advanced/optimizing-performance-controls?view=netframeworkdesktop-4.8&WT.mc_id=DT-MVP-5003472
-->
<!--
Because Data Virtualization is enabled on this tree view by default, if you don't bind the IsExpanded property to something in the bound view model,
you can loose the expanded state of items when the TreeListViewItem is recycled.
For more information:
https://github.com/MaterialDesignInXAML/MaterialDesignInXamlToolkit/issues/3640#issuecomment-2274086113
https://learn.microsoft.com/dotnet/desktop/wpf/advanced/optimizing-performance-controls?view=netframeworkdesktop-4.8&WT.mc_id=DT-MVP-5003472
-->
<materialDesign:TreeListView.ItemContainerStyle>
<Style TargetType="materialDesign:TreeListViewItem" BasedOn="{StaticResource {x:Type materialDesign:TreeListViewItem}}">
<Setter Property="IsExpanded" Value="{Binding IsExpanded}" />
</Style>
</materialDesign:TreeListView.ItemContainerStyle>

</materialDesign:TreeListView>
<StackPanel Orientation="Horizontal" VerticalAlignment="Bottom" HorizontalAlignment="Right">
<Button Command="{Binding AddListTreeItemCommand}"
Expand All @@ -258,11 +263,95 @@
</Grid>
</smtx:XamlDisplay>

<TextBlock Grid.Row="2"
<TextBlock Style="{StaticResource MaterialDesignHeadline6TextBlock}"
Text="Multi-Select Tree Grid View:"
Grid.Row="2"
Grid.Column="1"
Margin="0,40,0,0" />
<smtx:XamlDisplay Grid.Row="3"
Grid.Column="1"
VerticalContentAlignment="Top"
UniqueKey="trees_7"
Margin="0,20,0,20">
<Grid>
<materialDesign:TreeListView MinWidth="220"
MaxHeight="450"
ItemsSource="{Binding TreeItems}"
SelectedItem="{Binding SelectedTreeItem}">
<materialDesign:TreeListView.Resources>
<HierarchicalDataTemplate DataType="{x:Type domain:TestItem}"
ItemsSource="{Binding Items, Mode=OneTime}">
<TextBlock VerticalAlignment="Center"
Text="{Binding Name, Mode=OneTime}" />
</HierarchicalDataTemplate>

<HierarchicalDataTemplate DataType="{x:Type domain:MovieCategory}"
ItemsSource="{Binding Movies, Mode=OneTime}">
<TextBlock Margin="3,2"
Text="{Binding Name, Mode=OneTime}" />
</HierarchicalDataTemplate>

<DataTemplate DataType="{x:Type domain:Movie}">
<TextBlock Margin="3,2"
Text="{Binding Name, Mode=OneTime}"
ToolTip="{Binding Director, Mode=OneTime}" />
</DataTemplate>
</materialDesign:TreeListView.Resources>

<materialDesign:TreeListView.ItemContainerStyle>
<Style TargetType="materialDesign:TreeListViewItem"
BasedOn="{StaticResource {x:Type materialDesign:TreeListViewItem}}">
<Setter Property="IsExpanded"
Value="{Binding IsExpanded}" />
</Style>
</materialDesign:TreeListView.ItemContainerStyle>

<materialDesign:TreeListView.View>
<GridView>
<GridView.Columns>
<GridViewColumn Width="250"
Header="Name">
<GridViewColumn.CellTemplate>
<DataTemplate>
<TextBlock VerticalAlignment="Center"
Text="{Binding Name, Mode=OneTime}" />
</DataTemplate>
</GridViewColumn.CellTemplate>
</GridViewColumn>

<GridViewColumn Width="100"
Header="Count">
<GridViewColumn.CellTemplate>
<DataTemplate>
<TextBlock VerticalAlignment="Center"
Text="{Binding Items.Count, Mode=OneWay}" />
</DataTemplate>
</GridViewColumn.CellTemplate>
</GridViewColumn>
</GridView.Columns>
</GridView>
</materialDesign:TreeListView.View>
</materialDesign:TreeListView>
<StackPanel Orientation="Horizontal"
VerticalAlignment="Bottom"
HorizontalAlignment="Right">
<Button Command="{Binding AddListTreeItemCommand}"
ToolTip="Add an item"
Content="{materialDesign:PackIcon Kind=Add}" />

<Button Command="{Binding RemoveListTreeItemCommand}"
ToolTip="Remove selected item(s)"
Content="{materialDesign:PackIcon Kind=Remove}" />

</StackPanel>
</Grid>
</smtx:XamlDisplay>

<TextBlock Grid.Row="4"
Style="{StaticResource PageSectionTitleTextBlock}"
Text="Additional node content, syntax 1" />

<smtx:XamlDisplay Grid.Row="3"
<smtx:XamlDisplay Grid.Row="5"
UniqueKey="trees_4">
<TreeView>
<materialDesign:TreeViewAssist.AdditionalTemplate>
Expand Down Expand Up @@ -305,13 +394,13 @@
</TreeView>
</smtx:XamlDisplay>

<TextBlock Grid.Row="2"
<TextBlock Grid.Row="4"
Grid.Column="1"
Margin="32,0,0,0"
Style="{StaticResource PageSectionTitleTextBlock}"
Text="Additional node content, syntax 2" />

<smtx:XamlDisplay Grid.Row="3"
<smtx:XamlDisplay Grid.Row="5"
Grid.Column="1"
Margin="32,0,0,0"
UniqueKey="trees_5">
Expand Down Expand Up @@ -355,13 +444,13 @@
</TreeView>
</smtx:XamlDisplay>

<TextBlock Grid.Row="2"
<TextBlock Grid.Row="4"
Grid.Column="2"
Margin="32,0,0,0"
Style="{StaticResource PageSectionTitleTextBlock}"
Text="Additional node content, showcase" />

<smtx:XamlDisplay Grid.Row="3"
<smtx:XamlDisplay Grid.Row="5"
Grid.Column="2"
Margin="32,0,0,0"
UniqueKey="trees_6">
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
using System.Globalization;
using System.Windows.Data;

namespace MaterialDesignThemes.Wpf.Converters.Internal;

internal class ViewIsGridViewConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
// Returns true if value is a GridView, otherwise false
return value is GridView;
}

public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
throw new NotSupportedException();
}
}
Original file line number Diff line number Diff line change
@@ -1,13 +1,17 @@
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:converters="clr-namespace:MaterialDesignThemes.Wpf.Converters"
xmlns:wpf="clr-namespace:MaterialDesignThemes.Wpf"
xmlns:internal="clr-namespace:MaterialDesignThemes.Wpf.Internal">
xmlns:internalConverters="clr-namespace:MaterialDesignThemes.Wpf.Converters.Internal"
xmlns:internal="clr-namespace:MaterialDesignThemes.Wpf.Internal"
xmlns:wpf="clr-namespace:MaterialDesignThemes.Wpf">
<internalConverters:ViewIsGridViewConverter x:Key="ViewIsGridViewConverter" />

<Style x:Key="MaterialDesignTreeListViewToggleButtonStyle" TargetType="{x:Type ToggleButton}">
<Setter Property="Background" Value="Transparent" />
<Setter Property="Foreground" Value="{Binding Foreground, RelativeSource={RelativeSource AncestorType=wpf:TreeListViewItem}}" />
<Setter Property="Focusable" Value="False" />
<Setter Property="Height" Value="16" />
<Setter Property="Margin" Value="8,0,8,0" />
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type ToggleButton}">
Expand Down Expand Up @@ -78,6 +82,11 @@
</Canvas>
</Viewbox>
</Border>
<ControlTemplate.Triggers>
<DataTrigger Binding="{Binding RelativeSource={RelativeSource AncestorType={x:Type wpf:TreeListViewItem}}, Path=HasItems}" Value="false">
<Setter Property="Visibility" Value="Hidden" />
</DataTrigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</Setter.Value>
</Setter>
Expand All @@ -104,11 +113,14 @@
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type wpf:TreeListViewItem}">
<Grid>
<Grid x:Name="ItemGrid">
<Grid.Margin>
<MultiBinding Converter="{x:Static converters:TreeListViewIndentConverter.Instance}">
<Binding Path="LevelIndentSize" RelativeSource="{RelativeSource FindAncestor, AncestorType={x:Type wpf:TreeListView}}" FallbackValue="16" />
<Binding Path="Level" RelativeSource="{RelativeSource TemplatedParent}" />
<Binding Path="LevelIndentSize"
RelativeSource="{RelativeSource FindAncestor, AncestorType={x:Type wpf:TreeListView}}"
FallbackValue="16" />
<Binding Path="Level"
RelativeSource="{RelativeSource TemplatedParent}" />
</MultiBinding>
</Grid.Margin>
<Grid.ColumnDefinitions>
Expand Down Expand Up @@ -167,13 +179,11 @@
<ToggleButton x:Name="Expander"
Width="{TemplateBinding wpf:TreeViewAssist.ExpanderSize}"
Height="{TemplateBinding wpf:TreeViewAssist.ExpanderSize}"
Margin="8,0,8,0"
VerticalAlignment="Center"
ClickMode="Press"
Foreground="{TemplateBinding Foreground}"
IsChecked="{Binding IsExpanded, RelativeSource={RelativeSource TemplatedParent}}"
Style="{StaticResource MaterialDesignTreeListViewToggleButtonStyle}" />

<Border x:Name="MouseOverBorder"
Grid.Column="1"
Grid.ColumnSpan="2"
Expand All @@ -198,8 +208,18 @@
VerticalContentAlignment="{TemplateBinding VerticalContentAlignment}"
Feedback="{TemplateBinding Foreground, Converter={x:Static converters:BrushRoundConverter.Instance}}"
Focusable="False"
Foreground="{TemplateBinding Foreground}"
SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}">
<internal:TreeListViewContentPresenter x:Name="PART_ContentPresenter" ContentSource="Content" />
<Grid Margin="0,0,0,0">
<internal:TreeListViewContentPresenter x:Name="PART_ContentPresenter"
ContentSource="Content"
Visibility="Visible" />

<GridViewRowPresenter x:Name="PART_GridViewRowPresenterr"
VerticalAlignment="{TemplateBinding VerticalContentAlignment}"
SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}"
Visibility="Collapsed" />
</Grid>
</wpf:Ripple>
</Grid>

Expand All @@ -219,9 +239,6 @@
<DataTrigger Binding="{Binding RelativeSource={RelativeSource Self}, Path=(wpf:TreeViewAssist.AdditionalTemplateSelector), Converter={x:Static converters:NullableToVisibilityConverter.CollapsedInstance}, Mode=OneWay}" Value="Visible">
<Setter TargetName="AdditionalContentControl" Property="Visibility" Value="Visible" />
</DataTrigger>
<Trigger Property="HasItems" Value="false">
<Setter TargetName="Expander" Property="Visibility" Value="{Binding RelativeSource={RelativeSource TemplatedParent}, Path=(wpf:TreeViewAssist.HasNoItemsExpanderVisibility)}" />
</Trigger>
<Trigger Property="IsEnabled" Value="false">
<Setter Property="Opacity" Value=".56" />
</Trigger>
Expand All @@ -230,6 +247,12 @@
<Setter TargetName="Ripple" Property="Feedback" Value="Transparent" />
<Setter TargetName="SelectedBorder" Property="Visibility" Value="Collapsed" />
</DataTrigger>
<DataTrigger Binding="{Binding View, RelativeSource={RelativeSource AncestorType=wpf:TreeListView}, Converter={StaticResource ViewIsGridViewConverter}}" Value="True">
<Setter TargetName="PART_ContentPresenter" Property="Visibility" Value="Hidden" />
<Setter TargetName="PART_GridViewRowPresenterr" Property="Visibility" Value="Visible" />
<Setter TargetName="Expander" Property="Visibility" Value="Collapsed" />
<Setter TargetName="ItemGrid" Property="Margin" Value="0" />
</DataTrigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</Setter.Value>
Expand All @@ -245,6 +268,7 @@
<Setter Property="BorderBrush" Value="{x:Null}" />
<Setter Property="BorderThickness" Value="0" />
<Setter Property="Foreground" Value="{DynamicResource MaterialDesign.Brush.Foreground}" />
<Setter Property="HorizontalContentAlignment" Value="Stretch" />
<Setter Property="Padding" Value="1" />
<Setter Property="ScrollViewer.HorizontalScrollBarVisibility" Value="Auto" />
<Setter Property="ScrollViewer.PanningMode" Value="Both" />
Expand All @@ -257,6 +281,7 @@
BorderThickness="{TemplateBinding BorderThickness}"
SnapsToDevicePixels="true">
<ScrollViewer x:Name="PART_ScrollViewer"
Style="{DynamicResource {x:Static GridView.GridViewScrollViewerStyleKey}}"
wpf:ScrollViewerAssist.IgnorePadding="{Binding Path=(wpf:ScrollViewerAssist.IgnorePadding), RelativeSource={RelativeSource TemplatedParent}}"
wpf:ScrollViewerAssist.PaddingMode="{Binding Path=(wpf:ScrollViewerAssist.PaddingMode), RelativeSource={RelativeSource TemplatedParent}}"
Padding="{TemplateBinding Padding}"
Expand Down
Loading
Loading