Skip to content

implement TreeDataGrid #3877

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
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
43 changes: 32 additions & 11 deletions src/MainDemo.Wpf/Trees.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -214,9 +214,8 @@
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 +230,41 @@
</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.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}"
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,11 +1,14 @@
<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="Template">
Expand Down Expand Up @@ -78,6 +81,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 +112,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 @@ -173,7 +184,6 @@
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,19 @@
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"
Margin="{TemplateBinding Padding}"
VerticalAlignment="{TemplateBinding VerticalContentAlignment}"
SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}"
Visibility="Collapsed" />
</Grid>
</wpf:Ripple>
</Grid>

Expand All @@ -219,9 +240,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 +248,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 +269,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 +282,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
69 changes: 68 additions & 1 deletion src/MaterialDesignThemes.Wpf/TreeListView.cs
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
using System.Collections;
using System.Collections.Specialized;
using System.Windows.Automation.Peers;
using System.Windows.Data;

using MaterialDesignThemes.Wpf.Automation.Peers;
using MaterialDesignThemes.Wpf.Converters;
using MaterialDesignThemes.Wpf.Internal;

namespace MaterialDesignThemes.Wpf;

//TODO: Implement bindable property for getting selected items
//TODO: Implement GridView support for having columns
public class TreeListView : ListView
{
public double LevelIndentSize
Expand Down Expand Up @@ -259,4 +262,68 @@ internal void MoveSelectionToParent(TreeListViewItem item)
}
}
}

protected override void OnPropertyChanged(DependencyPropertyChangedEventArgs e)
{
base.OnPropertyChanged(e);

if (e.Property == ViewProperty)
{
if (View is GridView gridView)
{
AddToggleButtonToFirstColumn(gridView);
}
}
}

private void AddToggleButtonToFirstColumn(GridView gridView)
{
if (gridView.Columns.Count > 0)
{
var firstColumn = gridView.Columns[0];
firstColumn.CellTemplate = CreateToggleButtonTemplate(firstColumn.CellTemplate);
}
}

private DataTemplate CreateToggleButtonTemplate(DataTemplate originalTemplate)
{
var template = new DataTemplate();

var stackPanelFactory = new FrameworkElementFactory(typeof(StackPanel));
stackPanelFactory.SetValue(StackPanel.OrientationProperty, Orientation.Horizontal);

var marginMultiBinding = new MultiBinding
{
Converter = TreeListViewIndentConverter.Instance,
};
marginMultiBinding.Bindings.Add(new Binding(LevelIndentSizeProperty.Name)
{
RelativeSource = new RelativeSource(RelativeSourceMode.FindAncestor, typeof(TreeListView), 1)
});
marginMultiBinding.Bindings.Add(new Binding(TreeListViewItem.LevelProperty.Name)
{
RelativeSource = new RelativeSource(RelativeSourceMode.FindAncestor, typeof(TreeListViewItem), 1)
});
stackPanelFactory.SetBinding(StackPanel.MarginProperty, marginMultiBinding);

var toggleButtonFactory = new FrameworkElementFactory(typeof(ToggleButton));
toggleButtonFactory.SetValue(ToggleButton.StyleProperty, Application.Current.Resources["MaterialDesignTreeListViewToggleButtonStyle"]);

toggleButtonFactory.SetBinding(ToggleButton.IsCheckedProperty, new Binding(TreeListViewItem.IsExpandedProperty.Name)
{
RelativeSource = new RelativeSource(RelativeSourceMode.FindAncestor, typeof(TreeListViewItem), 1)
});

stackPanelFactory.AppendChild(toggleButtonFactory);

if (originalTemplate != null)
{
var originalContentFactory = new FrameworkElementFactory(typeof(ContentPresenter));
originalContentFactory.SetValue(ContentPresenter.ContentTemplateProperty, originalTemplate);
stackPanelFactory.AppendChild(originalContentFactory);
}

template.VisualTree = stackPanelFactory;
return template;
}
}