Generic Formatting and Alignment in XAML based on trigger - c#

I am trying to apply overall formatting on my application. The application is already build. When showing data in a datagrid, I want to format and right align all the textblocks having amount in them. Is this possible to do it using some trigger or any other way? Is it possible to know if a binding path contains word "amount" and if it does apply the required formatting.
Basically I want to control formatting and alignment based on values in whole app from one location. So that it is easy to change in longer run.
Here is my XAML for view:
<UserControl x:Class="CPOSApplication.UserControls.Sales.SalesFilterResult"
xmlns:PaginationControl="clr-namespace:CPOSApplication.GeneralControl.PaginationControl"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:localConvertor="clr-namespace:CPOSApplication.Convertors"
xmlns:com="pagina"
xmlns:local="clr-namespace:CPOSApplication.Convertors"
mc:Ignorable="d"
d:DesignHeight="300" d:DesignWidth="1000">
<UserControl.Resources>
<local:DataGridRowToIndexConvertor x:Key="RowToIndexConvertor">
</local:DataGridRowToIndexConvertor>
<localConvertor:PermissionToEnableConvertor x:Key="PermissionToBoolConvertor"/>
</UserControl.Resources>
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="Auto" />
<RowDefinition Height="4*" />
<RowDefinition Height="*" />
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*"/>
</Grid.ColumnDefinitions>
<Label x:Name="GridLabel" Grid.Row="0" Style="{DynamicResource HeadingLabelsCustomStyle}" Content="Sales"/>
<DataGrid Grid.Row="1" Style="{DynamicResource DataGridStyle}" ColumnHeaderStyle="{DynamicResource DataGridColumnHeaderStyle}" RowStyle="{DynamicResource DataGridRowStyle}" CellStyle="{DynamicResource DataGridCellStyle}" x:Name="SalesGrid">
<DataGrid.Columns>
<DataGridTemplateColumn Header="Id" Visibility="Collapsed" Width="Auto" IsReadOnly="True">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<TextBlock Text="{Binding Path=Id}" />
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
<DataGridTemplateColumn Header="Index" Width="*" IsReadOnly="True">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<TextBlock x:Name="IndexCell" Style="{DynamicResource GridTextBlock}" Text="{Binding RelativeSource={RelativeSource AncestorType=DataGridRow}, Converter={StaticResource RowToIndexConvertor}}" />
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
<DataGridTemplateColumn Header="Serial Number" Width="*" IsReadOnly="True">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<TextBlock Text="{Binding Path=SerialNumber}" Style="{DynamicResource GridTextBlock}" TextWrapping="NoWrap" TextTrimming="CharacterEllipsis"/>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
<DataGridTemplateColumn Header="Customer" Width="*" IsReadOnly="True" >
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<TextBlock Text="{Binding Path=CustomerName}" Style="{DynamicResource GridTextBlock}" TextWrapping="NoWrap" TextTrimming="CharacterEllipsis"/>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
<DataGridTemplateColumn Header="Payment Method" Width="*" IsReadOnly="True" >
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<TextBlock Text="{Binding Path=FormattedPaymentType}" Style="{DynamicResource GridTextBlock}" TextWrapping="NoWrap" TextTrimming="CharacterEllipsis" />
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
<DataGridTemplateColumn Header="Sale Date" Width="*" IsReadOnly="True" >
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<TextBlock Text="{Binding Path=CreatedDate}" Style="{DynamicResource GridTextBlock}" TextWrapping="NoWrap" TextTrimming="CharacterEllipsis"/>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
<DataGridTemplateColumn Header="Sale Amount" Width="*" IsReadOnly="True" >
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<TextBlock Text="{Binding Path=TotalAmount}" Style="{DynamicResource GridTextBlock}" TextWrapping="NoWrap" TextTrimming="CharacterEllipsis"/>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
<DataGridTemplateColumn Width="*" Header="Actions" IsReadOnly="True">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<StackPanel Style="{DynamicResource ActionPanel}">
<Image x:Name="EditImg" IsEnabled="{Binding Converter={StaticResource PermissionToBoolConvertor}, ConverterParameter='Edit Sale|Sale'}" Visibility="{Binding Converter={StaticResource LiveConnectionToVis}}" Style="{DynamicResource EditImg}" Source="/CPOSApplication;component/Resources/Images/Icons/edit.png" Tag="{Binding}" MouseDown="Edit_Click"></Image>
<Image x:Name="DeleteImg" IsEnabled="{Binding Converter={StaticResource PermissionToBoolConvertor}, ConverterParameter='Delete Sale|Sale'}" Visibility="{Binding Converter={StaticResource LiveConnectionToVis}}" Style="{DynamicResource DeleteImg}" Source="/CPOSApplication;component/Resources/Images/Icons/delete.png" Tag="{Binding}" MouseDown="Delete_Click"></Image>
</StackPanel>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
</DataGrid.Columns>
</DataGrid>
<PaginationControl:Paginator Background="#e4e4e4" x:Name="pager" Grid.Row="2" VerticalAlignment="Bottom" Margin="0,10,0,0" Height="30" />
</Grid>
</UserControl>

You can try binding TextBlocks like this:
<TextBlock Text="{Binding Path=TotalAmount}" TextAlignment="{Binding Path=TotalAmount, Converter="{StaticResource ContentToAlignmentConverter}" />
You will have to implement ContentToAlignmentConverter yourself. I won't put a code of it here, because it depends what you exactly want to check in it, but it's pretty easy to do it yourself. Here is a sample of a value converter in WPF
EDIT:
If you want to apply this changes to all your TextBlocks in your DataGrid, you should try something like this (not sure if it will work, but it should):
<DataGrid.Resources>
<Style TargetType="{x:Type TextBlock}" BasedOn="{DynamicResource GridTextBlock}">
<Setter Property="TextAlignment" Value="{Binding RelativeSource={RelativeSource Mode=Self}, Path=Text, Converter="{StaticResource ContentToAlignmentConverter}}" />
</Style>
</DataGrid.Resources>

Related

WPF DataGridCell of wrong type

I have a WPF datagrid that isn't behaving. When I click to edit the cell, the DataGridTemplateColumn.CellEditingTemplate is not displayed. If I click inside the cell and hit F2 to edit, the DataGridTemplateColumn.CellEditingTemplate is displayed as it should be. I was trying to make this a one click edit cell using the code from this SO post.
private void PendingDetailsDataGrid_GotFocus( object sender, RoutedEventArgs e )
{
if ( e.OriginalSource.GetType() == typeof( DataGridCell ) )
{
DataGrid grd = ( DataGrid ) sender;
grd.BeginEdit( e );
}
}
and I noticed that the if statement always fails because DataGridCell is of type Microsoft.Windows.Controls using assembly (WPFToolkit.dll) and the e.OriginalSource is of type System.Windows.Controls (Presentation.dll). If I hover over the DataGrid declaration in XAML view it shows System.Windows.Controls.DataGrid so how is the DataGridCell of the wrong type? I have an almost identical setup in another Window and I copied the DataGrid and the references from that form but nothing changed.
EDIT
<Controls:MetroWindow
x:Class="DFTI.Views.TransactionsWindow2"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:Controls="http://metro.mahapps.com/winfx/xaml/controls"
xmlns:vm="clr-namespace:DFTI.ViewModels"
xmlns:local="clr-namespace:DFTI.Views"
xmlns:toolkit="clr-namespace:System.Windows.Controls;assembly=System.Windows.Controls.Input.Toolkit"
xmlns:Dialog="clr-namespace:MahApps.Metro.Controls.Dialogs;assembly=MahApps.Metro"
xmlns:i="clr-namespace:System.Windows.Interactivity;assembly=System.Windows.Interactivity"
xmlns:cmd="clr-namespace:GalaSoft.MvvmLight.Command;assembly=GalaSoft.MvvmLight.Platform"
Dialog:DialogParticipation.Register="{Binding}"
Title="Transactions" Height="560" Width="900"
Name="Transactions"
WindowStartupLocation="CenterScreen">
<i:Interaction.Triggers>
<i:EventTrigger EventName="Closing">
<cmd:EventToCommand Command="{Binding Path=ClosingCommand}" PassEventArgsToCommand="True" />
</i:EventTrigger>
</i:Interaction.Triggers>
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="*"/>
<RowDefinition Height="Auto"/>
<RowDefinition Height="30"/>
</Grid.RowDefinitions>
<DataGrid Grid.Row="0"
x:Name="PendingTransactions"
DataGridCell.Selected="PendingDetailsDataGrid_GotFocus"
ItemsSource="{Binding PendingTransactions}"
SelectedItem="{Binding PendingDetails}"
AutoGenerateColumns="False"
CanUserResizeColumns="True"
CanUserDeleteRows="False"
CanUserReorderColumns="True"
Margin="10,0,10,17" >
<DataGrid.Columns>
<DataGridTextColumn Header="Posting Date" Width="Auto" Binding="{Binding PostingDate}" IsReadOnly="True" />
<DataGridTextColumn Header="Status" Width="Auto" Binding="{Binding Status}" IsReadOnly="True" />
<DataGridTextColumn Header="Batch No" Width="Auto" Binding="{Binding BatchNo}" IsReadOnly="True" />
</DataGrid.Columns>
</DataGrid>
<DataGrid Grid.Row="1"
Height="200"
ItemsSource="{Binding PendingDetails.Details}"
AutoGenerateColumns="False"
CanUserAddRows="True"
CanUserDeleteRows="True"
CanUserResizeColumns="True"
CanUserReorderColumns="True"
DataGridCell.Selected="PendingDetailsDataGrid_GotFocus"
Margin="10,0">
<DataGrid.Columns>
<DataGridTemplateColumn x:Name="AccountNum" Header="Account No" >
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<StackPanel>
<TextBox BorderThickness="0" Text="{Binding Path=Account.RawAccountNumber}"/>
</StackPanel>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
<DataGridTemplateColumn.CellEditingTemplate>
<DataTemplate>
<StackPanel>
<toolkit:AutoCompleteBox
Background="AliceBlue"
IsTextCompletionEnabled="True"
FilterMode="Contains"
MinimumPrefixLength="2"
ValueMemberPath="RawAccountNumber"
PreviewTextInput="AutoCompleteBox_PreviewTextInput"
SelectedItem="{Binding Account, Mode=TwoWay}"
Text="{Binding Path=Account.RawAccountNumber}"
ItemsSource="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type Window}}, Path=DataContext.Accounts}" >
<toolkit:AutoCompleteBox.ItemTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal" >
<TextBlock Text="{Binding Path=RawAccountNumber}" FontWeight="Bold" Width="100"/>
<TextBlock Text="{Binding Path=Description}" />
</StackPanel>
</DataTemplate>
</toolkit:AutoCompleteBox.ItemTemplate>
</toolkit:AutoCompleteBox>
</StackPanel>
</DataTemplate>
</DataGridTemplateColumn.CellEditingTemplate>
</DataGridTemplateColumn>
<DataGridTextColumn Header="Description" Width="Auto" Binding="{Binding Description}" IsReadOnly="True" />
<DataGridTemplateColumn x:Name="DebitAmt" Header="Debit">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<TextBox Text="{Binding DAmount, StringFormat='$#,##0.00;$(#,##0.00)'}"
Width="100"
BorderThickness="0">
</TextBox>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
<DataGridTemplateColumn.CellEditingTemplate>
<DataTemplate>
<Controls:NumericUpDown Value="{Binding DAmount, Mode=TwoWay}" StringFormat="C2" Minimum="0" />
</DataTemplate>
</DataGridTemplateColumn.CellEditingTemplate>
</DataGridTemplateColumn>
<DataGridTemplateColumn x:Name="CreditAmt" Header="Credit">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<TextBox Text="{Binding CAmount, StringFormat='$#,##0.00;$(#,##0.00)'}"
Width="100"
BorderThickness="0">
</TextBox>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
<DataGridTemplateColumn.CellEditingTemplate>
<DataTemplate>
<Controls:NumericUpDown Value="{Binding CAmount, Mode=TwoWay}" StringFormat="C2" Minimum="0" />
</DataTemplate>
</DataGridTemplateColumn.CellEditingTemplate>
</DataGridTemplateColumn>
</DataGrid.Columns>
</DataGrid>
</Grid>
</Controls:MetroWindow >
Trt to use the fully qualified type name in the if statement. You should not need to involve WPFToolkit.dll here. This works for me:
private void PendingDetailsDataGrid_GotFocus(object sender, RoutedEventArgs e)
{
if (e.OriginalSource.GetType() == typeof(System.Windows.Controls.DataGridCell))
{
DataGrid grd = (DataGrid)sender;
grd.BeginEdit(e);
}
}

Fit an Picture in a DataGrid

I'm trying to put a picture in my DataGrid. So I used a DataGridTemplateColumn but the Result is "glitchy".
Here is my wpf extract code :
<DataGrid MinRowHeight="24" x:Name="ListFamily" Grid.Column="1"
Margin="2" VerticalContentAlignment="Stretch"
HorizontalContentAlignment="Stretch"
AutoGenerateColumns="False" IsReadOnly="True"
SelectionChanged="ListFamily_SelectionChanged"
CanUserReorderColumns="False"
CanUserSortColumns="False" Grid.Row="1">
<DataGrid.Columns>
<!-- some datagridcolumn -->
<DataGridTemplateColumn Width="Auto" CanUserResize="False" IsReadOnly="True">
<DataGridTemplateColumn.Header>
<Label Content="{x:Static res:StringResources.language}" ToolTip="{x:Static tt:TooltipStrings.language}" HorizontalContentAlignment="Stretch"/>
</DataGridTemplateColumn.Header>
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<Image Source="{Binding Language, Converter={StaticResource StringToPicture}}" Height="{Binding RowHeight, ElementName=ListFamily}" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" Stretch="Uniform"/>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
<DataGridTemplateColumn.CellStyle>
<Style>
<Setter Property="FrameworkElement.VerticalAlignment" Value="Center"/>
</Style>
</DataGridTemplateColumn.CellStyle>
</DataGridTemplateColumn>
</DataGrid.Columns>
</DataGrid>
and here is what I get :
I triend to bind RowHeight of the DataGrid but without luck. Any ideas ?
Thanks
Thibaud
Using this code is working :
<DataGridTemplateColumn Width="Auto" CanUserResize="False" IsReadOnly="True">
<DataGridTemplateColumn.Header>
<Label Content="{x:Static res:StringResources.language}" ToolTip="{x:Static tt:TooltipStrings.language}" HorizontalContentAlignment="Stretch"/>
</DataGridTemplateColumn.Header>
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<Image Source="{Binding Language, Converter={StaticResource StringToPicture}}" Stretch="Uniform"/>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>

C# WPF Static item and binding combobox

I am trying to do some simple thing in WPF but can't find the way to do it.
I have a ComboBox in a DataGrid header to filter data. The data is binded to a GrouBy statement of all my data.
These show some CheckBox The XAML code is:
<DataGridTextColumn x:Name="Type"
Binding="{Binding TypeOfData, Mode=OneTime}"
SortMemberPath="TypeOfData"
IsReadOnly="true"
CanUserSort="true">
<DataGridTextColumn.Header>
<DockPanel>
<Label Content="Type Of Data"
DockPanel.Dock="Left"/>
<ComboBox x:Name="comboBoxType"
DockPanel.Dock="Right"
SelectionChanged="comboBoxType_SelectionChanged">
<ComboBox.ItemTemplate>
<DataTemplate>
<StackPanel x:Name="itemsComboBox">
<CheckBox Name="checkBoxType"
IsChecked="False"
Content="{Binding Key}"
Unchecked="FilterChange"
Checked="FilterChange"/>
</StackPanel>
</DataTemplate>
</ComboBox.ItemTemplate>
</ComboBox>
</DockPanel>
</DataGridTextColumn.Header>
</DataGridTextColumn>
The code behind fore binding is:
comboBoxType.ItemsSource = allData.GroupBy(data=> data.TypeOfData).OrderBy(grp=> grp.Key);
And this work.
But now I want to add 2 button to check and uncheck all at the end or at the start of the ComboBox but I can't seem to find how to add those static button in a dynamic data template.
Edit Answer to grek40:
I want the buttons in the combobox Items before or after the checkboxes.
CompositeCollection could help but I can't seem to make it work.
Thank You
Replace the DockPanel with a Grid that has several ColumnDefinitions:
<DataGridTextColumn x:Name="Type" Binding="{Binding TypeOfData, Mode=OneTime}" SortMemberPath="TypeOfData" IsReadOnly="true" CanUserSort="true">
<DataGridTextColumn.Header>
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto" />
<ColumnDefinition Width="*" />
<ColumnDefinition Width="Auto" />
</Grid.ColumnDefinitions>
<Label Content="Type Of Data" />
<ComboBox x:Name="comboBoxType"
Grid.Column="1"
SelectionChanged="comboBoxType_SelectionChanged">
<ComboBox.ItemTemplate>
<DataTemplate>
<StackPanel x:Name="itemsComboBox">
<CheckBox Name="checkBoxType"
IsChecked="False"
Content="{Binding Key}"
Unchecked="FilterChange"
Checked="FilterChange"/>
</StackPanel>
</DataTemplate>
</ComboBox.ItemTemplate>
</ComboBox>
<Button Content="Check"
Grid.Column="2" />
</Grid>
</DataGridTextColumn.Header>
</DataGridTextColumn>

StackPanel in a DataGrid doesn't collapse when it should

I have a problem with a StackPanel and its visibility in a DatagridCellTemplateColumn.
My ControlTemplate looks like this:
<ControlTemplate x:Key="PartPrioritySettingsGridCellTemplate">
<StackPanel Name="CellGrid" Orientation="Horizontal" Tag="{TemplateBinding Tag}"
Visibility="{Binding RelativeSource={RelativeSource Self},
Path=Tag.IsNotEmpty, Converter={StaticResource BoolToVisibility}}">
<CheckBox Margin="4,1,2,1"
IsChecked="{Binding Path=Tag.Items.IsChecked, ElementName=CellGrid, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" />
<xceed:IntegerUpDown Margin="2,1" Maximum="99" Minimum="1"
Value="{Binding Path=Tag.Priority, ElementName=CellGrid, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" />
<Button Margin="2,1,4,1" Command="{Binding Path=Tag.ShowDetailCommand, ElementName=CellGrid}"
Content="{Binding Path=Tag.ItemsCountString, ElementName=CellGrid}"
ContentStringFormat="{}{0:}" />
</StackPanel>
</ControlTemplate>
Usage of this template looks like this:
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<Control Width="auto" Tag="{Binding Webs}" Template="{StaticResource PartPrioritySettingsGridCellTemplate}" />
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
I want to make a DataGrid where the width of all columns is set to the width of the current content. But when the content of a column is collapsed, then the width of a column should be given only by the width of the header. Actually I don't know what is the problem in my solution, but all my columns have the same width, even if their content is collapsed.
What I'm trying to achieve looks like this:
What I'm getting looks like this:
And this is code of the DataGrid
<DataGrid Grid.Row="0"
AutoGenerateColumns="False"
CanUserAddRows="False"
CanUserDeleteRows="False"
CanUserReorderColumns="False"
CanUserResizeColumns="False"
CanUserResizeRows="False"
ItemsSource="{Binding JobBomPriorityRows,
UpdateSourceTrigger=PropertyChanged}">
<!-- -->
<DataGrid.Resources>
<vc:BoolToVisibilityConverter x:Key="BoolToVisibility" />
<ControlTemplate x:Key="PartPrioritySettingsGridCellTemplate" TargetType="Control">
<StackPanel Name="CellGrid"
Width="auto"
Orientation="Horizontal"
Tag="{TemplateBinding Tag}"
Visibility="{Binding RelativeSource={RelativeSource Self},
Path=Tag.IsNotEmpty,
Converter={StaticResource BoolToVisibility}}">
<CheckBox Margin="4,1,2,1" IsChecked="{Binding Path=Tag.Items.IsChecked, ElementName=CellGrid, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" />
<xceed:IntegerUpDown Margin="2,1"
Maximum="99"
Minimum="1"
Value="{Binding Path=Tag.Priority,
ElementName=CellGrid,
Mode=TwoWay,
UpdateSourceTrigger=PropertyChanged}" />
<Button Margin="2,1,4,1"
Command="{Binding Path=Tag.ShowDetailCommand,
ElementName=CellGrid}"
Content="{Binding Path=Tag.ItemsCountString,
ElementName=CellGrid}"
ContentStringFormat="{}{0:}"
Padding="3,0" />
</StackPanel>
</ControlTemplate>
</DataGrid.Resources>
<DataGrid.Columns>
<DataGridTextColumn Width="*"
Binding="{Binding Week}"
Header="Week"
IsReadOnly="True"
SortDirection="Ascending" />
<DataGridTextColumn Width="*"
Binding="{Binding JobBom}"
Header="Job Bom"
IsReadOnly="True" />
<DataGridTemplateColumn Width="auto"
Header="Web"
IsReadOnly="True">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<Control Tag="{Binding Webs}" Template="{StaticResource PartPrioritySettingsGridCellTemplate}" />
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
<DataGridTemplateColumn Width="auto"
Header="Flange"
IsReadOnly="True">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<Control Tag="{Binding Flanges}" Template="{StaticResource PartPrioritySettingsGridCellTemplate}" />
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
<DataGridTemplateColumn Width="auto"
Header="Plate"
IsReadOnly="True">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<Control Tag="{Binding Plates}" Template="{StaticResource PartPrioritySettingsGridCellTemplate}" />
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
<DataGridTemplateColumn Width="auto"
Header="Gusset"
IsReadOnly="True">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<Control Tag="{Binding Gussets}" Template="{StaticResource PartPrioritySettingsGridCellTemplate}" />
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
<DataGridTemplateColumn Width="auto"
Header="SP"
IsReadOnly="True">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<Control Tag="{Binding SPs}" Template="{StaticResource PartPrioritySettingsGridCellTemplate}" />
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
<DataGridTemplateColumn Width="auto"
Header="STD G"
IsReadOnly="True">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<Control Tag="{Binding STD_Gs}" Template="{StaticResource PartPrioritySettingsGridCellTemplate}" />
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
<DataGridTemplateColumn Width="auto"
Header="STD SP"
IsReadOnly="True">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<Control Tag="{Binding STD_SPs}" Template="{StaticResource PartPrioritySettingsGridCellTemplate}" />
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
</DataGrid.Columns>
</DataGrid>

Formatting rows in a datagrid to look blank

Essentially, I am using a datagrid in WPF. If there is a row that contains no data, I want it to be a blank row and I want it to display as below:
However I am having real trouble trying to achieve this. I can do it by creating separate datagrids and splitting the data into each with a converter but it becomes very messy and makes the code hard to maintain.
I can also do it by leaving the row blank which looks as below:
However that does not look quite how I would like. Any help is appreciated and thanks in advance.
Code for the datagrid:
<DataGrid Width="265" RowHeight="11.5" CanUserAddRows="False" ItemsSource="{Binding Coupon.SelectionAreas[0].SelectionRows
,Converter={StaticResource DateTimeToSplitDataGridConverter}
,ConverterParameter='Table13Bet'}" AutoGenerateColumns="False" Background="Transparent" RowBackground="Transparent" HeadersVisibility="None" BorderBrush="LightGray" BorderThickness="1,0,1,0" AlternatingRowBackground="{x:Null}" GridLinesVisibility="Horizontal" IsReadOnly="True" SelectionMode="Single" >
<DataGrid.Columns>
<DataGridTemplateColumn Width="27">
<DataGridTemplateColumn.CellTemplate >
<DataTemplate DataType="r:SelectionRow">
<TextBlock Text="{Binding ExpectedOffDate,StringFormat='HH:mm'}" VerticalAlignment="Top" FontFamily="Arial Narrow" FontSize="7.8" HorizontalAlignment="Center" Foreground="{Binding Path=ColourForeground, Converter={StaticResource HexToForegroundColourConverter}}"
Background="{Binding Path=ColourBackground, Converter={StaticResource HexToBackgroundColourConverter}}" />
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
<DataGridTemplateColumn Width="27" >
<DataGridTemplateColumn.CellTemplate>
<DataTemplate DataType="r:SelectionRow">
<TextBlock Text="{Binding SelectionHome.Odd, Converter={StaticResource ChangeZeroToOneOddsToTbcConverter}}" FontFamily="Arial Narrow" VerticalAlignment="Top" FontSize="7.8" HorizontalAlignment="Center" Background="{Binding Path=ColourBackground, Converter={StaticResource HexToBackgroundColourConverter}}" />
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
<DataGridTemplateColumn Width="80" >
<DataGridTemplateColumn.CellTemplate>
<DataTemplate DataType="r:SelectionRow">
<TextBlock Text="{Binding SelectionHome.DisplayName, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" VerticalAlignment="Top" FontFamily="Arial Narrow" FontSize="7.8" HorizontalAlignment="Left" Background="{Binding Path=ColourBackground, Converter={StaticResource HexToBackgroundColourConverter}}" />
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
<DataGridTemplateColumn Width="24">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate DataType="r:SelectionRow">
<TextBlock Text="{Binding SelectionDraw.Odd, Converter={StaticResource ChangeZeroToOneOddsToTbcConverter}}" VerticalAlignment="Top" FontFamily="Arial Narrow" FontSize="7.8" HorizontalAlignment="Center" Background="{Binding Path=ColourBackground, Converter={StaticResource HexToBackgroundColourConverter}}" />
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
<DataGridTemplateColumn Width="80" >
<DataGridTemplateColumn.CellTemplate>
<DataTemplate DataType="r:SelectionRow">
<TextBlock Text="{Binding SelectionAway.DisplayName, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" VerticalAlignment="Top" FontFamily="Arial Narrow" FontSize="7.8" HorizontalAlignment="Right" Background="{Binding Path=ColourBackground, Converter={StaticResource HexToBackgroundColourConverter}}" />
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
<DataGridTemplateColumn Width="25" >
<DataGridTemplateColumn.CellTemplate>
<DataTemplate DataType="r:SelectionRow">
<TextBlock Text="{Binding SelectionAway.Odd, Converter={StaticResource ChangeZeroToOneOddsToTbcConverter}}" VerticalAlignment="Top" FontFamily="Arial Narrow" FontSize="7.8" HorizontalAlignment="Center" Background="{Binding Path=ColourBackground, Converter={StaticResource HexToBackgroundColourConverter}}" />
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
</DataGrid.Columns>
</DataGrid>
it is possible to display empty RowDetails under certain rows
<DataGrid>
<DataGrid.RowDetailsTemplate>
<DataTemplate>
<Grid Height="20"/>
</DataTemplate>
</DataGrid.RowDetailsTemplate>
<DataGrid.RowStyle>
<Style TargetType="DataGridRow">
<Setter Property="DetailsVisibility"
Value="{Binding Path=ShowEmptyRow,
Converter={StaticResource BoolToVisible}}"/>
</Style>
</DataGrid.RowStyle>
<DataGrid>
ShowEmptyRow is some bool property which indicates that there should be an empty space under current DataGridRow

Categories

Resources