Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Added template column example
Fixed issue where it would not switch to edit on click
  • Loading branch information
Keboo committed Aug 30, 2022
commit 0fc56350f595ed540fed8a8df2ddd08feac094f3
43 changes: 27 additions & 16 deletions MainDemo.Wpf/DataGrids.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,15 @@
d:DesignHeight="300"
d:DesignWidth="600"
d:DataContext="{d:DesignInstance domain:ListsAndGridsViewModel}">

<UserControl.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="pack://application:,,,/MaterialDesignThemes.Wpf;component/Themes/MaterialDesignTheme.DataGrid.xaml" />
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</UserControl.Resources>

<StackPanel>
<StackPanel Orientation="Horizontal">
<TextBlock
Expand All @@ -31,7 +31,7 @@
Text="SelectionUnit:"/>
<ComboBox x:Name="selectionUnitComboBox" ItemsSource="{Binding SelectionUnits}" SelectedIndex="0" VerticalAlignment="Center" />
</StackPanel>

<smtx:XamlDisplay UniqueKey="grids_1">
<DataGrid
ItemsSource="{Binding Items1}"
Expand All @@ -43,7 +43,7 @@
x:Key="DataContextProxy"
Data="{Binding}" />
</DataGrid.Resources>

<DataGrid.Columns>
<DataGridCheckBoxColumn
Binding="{Binding IsSelected, UpdateSourceTrigger=PropertyChanged}"
Expand All @@ -55,20 +55,20 @@
IsChecked="{Binding Data.IsAllItems1Selected, Source={StaticResource DataContextProxy}}"/>
</Border>
</DataGridCheckBoxColumn.Header>

<DataGridCheckBoxColumn.HeaderStyle>
<Style TargetType="{x:Type DataGridColumnHeader}" BasedOn="{StaticResource MaterialDesignDataGridColumnHeader}">
<Setter Property="HorizontalContentAlignment" Value="Center" />
</Style>
</DataGridCheckBoxColumn.HeaderStyle>
</DataGridCheckBoxColumn>

<DataGridTextColumn
Binding="{Binding Code}"
Header="Code"
ElementStyle="{StaticResource MaterialDesignDataGridTextColumnStyle}"
EditingElementStyle="{StaticResource MaterialDesignDataGridTextColumnEditingStyle}"/>

<!-- if you want to use the pop up style (MaterialDesignDataGridTextColumnPopupEditingStyle), you must use MaterialDataGridTextColumn -->
<materialDesign:DataGridTextColumn
Header="Name"
Expand All @@ -82,7 +82,7 @@
</Binding>
</materialDesign:DataGridTextColumn.Binding>
</materialDesign:DataGridTextColumn>

<!-- set a max length to get an indicator in the editor -->
<DataGridTextColumn
Header="Description"
Expand All @@ -96,7 +96,7 @@
</Binding>
</DataGridTextColumn.Binding>
</DataGridTextColumn>

<materialDesign:DataGridTextColumn
Binding="{Binding Numeric}"
Header="Number with long header"
Expand All @@ -117,7 +117,7 @@
</Setter>
</Style>
</DataGridTextColumn.HeaderStyle>

<DataGridTextColumn.ElementStyle>
<Style TargetType="{x:Type TextBlock}" BasedOn="{StaticResource MaterialDesignDataGridTextColumnStyle}">
<Setter Property="HorizontalAlignment" Value="Right" />
Expand All @@ -142,9 +142,20 @@
<materialDesign:DataGridComboBoxColumn
Header="ComboBox with long list"
SelectedValueBinding="{Binding Files}"
ItemsSourceBinding="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type DataGrid}}, Path=DataContext.Files}">
ItemsSourceBinding="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type DataGrid}}, Path=DataContext.Files}" />

</materialDesign:DataGridComboBoxColumn>
<DataGridTemplateColumn Header="Template Column">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate DataType="{x:Type domain:SelectableViewModel}">
<TextBlock Text="{Binding Name}" FontStyle="Italic" FontSize="14" />
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
<DataGridTemplateColumn.CellEditingTemplate>
<DataTemplate DataType="{x:Type domain:SelectableViewModel}">
<TextBox Text="{Binding Name}" Foreground="{DynamicResource SecondaryHueMidBrush}" />
</DataTemplate>
</DataGridTemplateColumn.CellEditingTemplate>
</DataGridTemplateColumn>
</DataGrid.Columns>
</DataGrid>
</smtx:XamlDisplay>
Expand All @@ -153,24 +164,24 @@
<materialDesign:PackIcon Kind="Information" Margin="0 0 5 0"/>
<TextBlock>DataGridComboBoxColumns are virtualized by default in the library</TextBlock>
</StackPanel>

<TextBlock
Style="{StaticResource MaterialDesignHeadline5TextBlock}"
Text="Auto Generated Columns"
Margin="0 24 0 0"/>

<smtx:XamlDisplay UniqueKey="grids_2">
<DataGrid
ItemsSource="{Binding Items2}"
CanUserAddRows="False"
SelectionUnit="Cell"
SelectionMode="Extended" />
</smtx:XamlDisplay>

<TextBlock
Style="{StaticResource MaterialDesignHeadline6TextBlock}"
Text="Custom Padding" Margin="0 24 0 0"/>

<smtx:XamlDisplay UniqueKey="grids_3">
<DataGrid
ItemsSource="{Binding Items3}"
Expand Down
101 changes: 50 additions & 51 deletions MainDemo.Wpf/Domain/SelectableViewModel.cs
Original file line number Diff line number Diff line change
@@ -1,55 +1,54 @@
namespace MaterialDesignDemo.Domain
namespace MaterialDesignDemo.Domain;

public class SelectableViewModel : ViewModelBase
{
public class SelectableViewModel : ViewModelBase
private bool _isSelected;
private string? _name;
private string? _description;
private char _code;
private double _numeric;
private string? _food;
private string? _files;

public bool IsSelected
{
get => _isSelected;
set => SetProperty(ref _isSelected, value);
}

public char Code
{
get => _code;
set => SetProperty(ref _code, value);
}

public string? Name
{
get => _name;
set => SetProperty(ref _name, value);
}

public string? Description
{
get => _description;
set => SetProperty(ref _description, value);
}

public double Numeric
{
get => _numeric;
set => SetProperty(ref _numeric, value);
}

public string? Food
{
get => _food;
set => SetProperty(ref _food, value);
}

public string? Files
{
private bool _isSelected;
private string? _name;
private string? _description;
private char _code;
private double _numeric;
private string? _food;
private string? _files;

public bool IsSelected
{
get => _isSelected;
set => SetProperty(ref _isSelected, value);
}

public char Code
{
get => _code;
set => SetProperty(ref _code, value);
}

public string? Name
{
get => _name;
set => SetProperty(ref _name, value);
}

public string? Description
{
get => _description;
set => SetProperty(ref _description, value);
}

public double Numeric
{
get => _numeric;
set => SetProperty(ref _numeric, value);
}

public string? Food
{
get => _food;
set => SetProperty(ref _food, value);
}

public string? Files
{
get => _files;
set => SetProperty(ref _files, value);
}
get => _files;
set => SetProperty(ref _files, value);
}
}
Loading