Skip to content

Commit 8e2c6a7

Browse files
committed
added SelectedItems demo for MultiSelectTreeView to MD3 app
1 parent b71860e commit 8e2c6a7

File tree

2 files changed

+68
-22
lines changed

2 files changed

+68
-22
lines changed

src/MaterialDesign3.Demo.Wpf/Domain/TreesViewModel.cs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,10 @@ public sealed class TreesViewModel : ViewModelBase
9090

9191
public AnotherCommandImplementation RemoveListTreeItemCommand { get; }
9292

93+
public AnotherCommandImplementation RemoveSelectedListTreeItemCommand { get; }
94+
95+
public ObservableCollection<TestItem?> SelectedTreeItems { get; } = [];
96+
9397
public TestItem? SelectedTreeItem
9498
{
9599
get => _selectedTreeItem;
@@ -210,6 +214,14 @@ static TestItem CreateTestItem(Random random, int depth)
210214
}
211215
},
212216
_ => SelectedItem != null);
217+
218+
RemoveSelectedListTreeItemCommand = new(item =>
219+
{
220+
if (item is TestItem treeItem)
221+
{
222+
SelectedTreeItems.Remove(treeItem);
223+
}
224+
});
213225
}
214226

215227
private static string GenerateString(int length)

src/MaterialDesign3.Demo.Wpf/Trees.xaml

Lines changed: 56 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -175,35 +175,69 @@
175175
</Grid>
176176
</smtx:XamlDisplay>
177177

178-
<TextBlock Style="{StaticResource MaterialDesignHeadline6TextBlock}" Text="Multi-Select Tree View:"
179-
Grid.Column="2"/>
178+
<TextBlock Grid.Column="2"
179+
Style="{StaticResource MaterialDesignHeadline6TextBlock}"
180+
Text="Multi-Select Tree View:" />
180181
<smtx:XamlDisplay Grid.Row="1"
181182
Grid.Column="2"
182183
VerticalContentAlignment="Top"
183184
UniqueKey="trees_3">
184-
<Grid>
185-
<materialDesign:TreeListView MinWidth="220" MaxHeight="450"
186-
ItemsSource="{Binding TreeItems}"
187-
SelectedItem="{Binding SelectedTreeItem}">
188-
<materialDesign:TreeListView.ItemTemplate>
189-
<HierarchicalDataTemplate DataType="{x:Type domain:TestItem}"
190-
ItemsSource="{Binding Items, Mode=OneWay}">
191-
<TextBlock Margin="3,2" Text="{Binding Name, Mode=OneWay}" />
192-
</HierarchicalDataTemplate>
193-
</materialDesign:TreeListView.ItemTemplate>
185+
<StackPanel Orientation="Horizontal">
186+
<Grid>
187+
<materialDesign:TreeListView MinWidth="220"
188+
MaxHeight="450"
189+
ItemsSource="{Binding TreeItems}"
190+
SelectedItem="{Binding SelectedTreeItem}"
191+
SelectedItems="{Binding SelectedTreeItems}">
192+
<materialDesign:TreeListView.ItemTemplate>
193+
<HierarchicalDataTemplate DataType="{x:Type domain:TestItem}" ItemsSource="{Binding Items, Mode=OneWay}">
194+
<TextBlock Margin="3,2" Text="{Binding Name, Mode=OneWay}" />
195+
</HierarchicalDataTemplate>
196+
</materialDesign:TreeListView.ItemTemplate>
197+
198+
</materialDesign:TreeListView>
199+
<StackPanel HorizontalAlignment="Right"
200+
VerticalAlignment="Bottom"
201+
Orientation="Horizontal">
202+
<Button Command="{Binding AddListTreeItemCommand}"
203+
Content="{materialDesign:PackIcon Kind=Add}"
204+
ToolTip="Add an item" />
205+
206+
<Button Command="{Binding RemoveListTreeItemCommand}"
207+
Content="{materialDesign:PackIcon Kind=Remove}"
208+
ToolTip="Remove selected item(s)" />
194209

195-
</materialDesign:TreeListView>
196-
<StackPanel Orientation="Horizontal" VerticalAlignment="Bottom" HorizontalAlignment="Right">
197-
<Button Command="{Binding AddListTreeItemCommand}"
198-
ToolTip="Add an item"
199-
Content="{materialDesign:PackIcon Kind=Add}"/>
210+
</StackPanel>
211+
</Grid>
212+
213+
<GroupBox Margin="4,0,0,0" Header="TreeListView.SelectedItems">
214+
<ListBox HorizontalContentAlignment="Stretch" ItemsSource="{Binding SelectedTreeItems}">
215+
<ListBox.ItemTemplate>
216+
<DataTemplate>
217+
<Grid>
218+
<Grid.ColumnDefinitions>
219+
<ColumnDefinition Width="*" />
220+
<ColumnDefinition Width="auto" />
221+
</Grid.ColumnDefinitions>
222+
<TextBlock Grid.Column="0"
223+
VerticalAlignment="Center"
224+
Text="{Binding Name}" />
225+
<Button Grid.Column="1"
226+
Width="{Binding Path=ActualHeight, RelativeSource={RelativeSource Mode=Self}}"
227+
Padding="4"
228+
Command="{Binding DataContext.RemoveSelectedListTreeItemCommand, RelativeSource={RelativeSource AncestorType=ListBox}}"
229+
CommandParameter="{Binding .}"
230+
Content="{materialDesign:PackIcon Kind=Bin}"
231+
Foreground="Red"
232+
Style="{StaticResource MaterialDesignFlatButton}" />
233+
</Grid>
234+
</DataTemplate>
235+
</ListBox.ItemTemplate>
236+
</ListBox>
237+
</GroupBox>
200238

201-
<Button Command="{Binding RemoveListTreeItemCommand}"
202-
ToolTip="Remove selected item(s)"
203-
Content="{materialDesign:PackIcon Kind=Remove}"/>
239+
</StackPanel>
204240

205-
</StackPanel>
206-
</Grid>
207241
</smtx:XamlDisplay>
208242

209243
<TextBlock Grid.Row="2"

0 commit comments

Comments
 (0)