Setting DataGrid Cell Style From Code Behind
-
I have a UserControl with a data grid that I'm creating in code in a View Model. It looks like this [[^]](https://1drv.ms/u/s!AjBmoYAYz\_v2hBe-082OOoYPAw3r?e=q67bBY). I'm trying to set the Cell Style to show 2 decimal from the VM but you can see it's not working. The first column is a string, and the remainder are Decimal. Here's my Cell Style
<Style x:Key="dataGridCurrencyCellStyle"
BasedOn="{StaticResource dataGridCellStyle}"
TargetType="{x:Type DataGridCell}"><Setter Property="Width" Value="75"/> <Setter Property="TextBlock.TextAlignment" Value="Right"/> <Setter Property="TextBlock.Text" Value="{Binding StringFormat={}{0:0.##}}"/> <Setter Property="IsEnabled" Value="{Binding DataContext.AreFieldsEnabled, ElementName=budgetControl}"/>
</Style>
Here's how I'm creating the table in the VM and setting the Style. The Style is set in the foreach loop towards the bottom.
private void LoadTable()
{
if (Budgets.Count > 0)
{
// 1- Create the data tableBudgetTable = null; BudgetTable = new DataTable(); // Add the first column DataColumn column = new DataColumn(); column.ColumnName = "Type"; column.DataType = System.Type.GetType("System.String"); column.ReadOnly = false; BudgetTable.Columns.Add(column); // Get a distinct list of Plan/Elevations List<string> planElevations = new List<string>(); switch (BudgetType) { case BudgetType.Job: var sheets = JobSequenceSheets.Where(x => x.Status == "New").ToList(); planElevations = sheets.Select(x => $"{x.Plan}{x.Elevation}").ToList(); break; case BudgetType.Project: planElevations = ProjectPlanTypeSummaries.Select(x => $"{x.Plan}{x.Elevation}").ToList(); break; default: throw new ArgumentException("Unhandled CASE statement"); break; } // Add a column for every distinct Plan/Elevation foreach (var planElevation in planElevations) { column = new DataColumn(); column.ColumnName = planElevation; column.Caption = planEle
-
I have a UserControl with a data grid that I'm creating in code in a View Model. It looks like this [[^]](https://1drv.ms/u/s!AjBmoYAYz\_v2hBe-082OOoYPAw3r?e=q67bBY). I'm trying to set the Cell Style to show 2 decimal from the VM but you can see it's not working. The first column is a string, and the remainder are Decimal. Here's my Cell Style
<Style x:Key="dataGridCurrencyCellStyle"
BasedOn="{StaticResource dataGridCellStyle}"
TargetType="{x:Type DataGridCell}"><Setter Property="Width" Value="75"/> <Setter Property="TextBlock.TextAlignment" Value="Right"/> <Setter Property="TextBlock.Text" Value="{Binding StringFormat={}{0:0.##}}"/> <Setter Property="IsEnabled" Value="{Binding DataContext.AreFieldsEnabled, ElementName=budgetControl}"/>
</Style>
Here's how I'm creating the table in the VM and setting the Style. The Style is set in the foreach loop towards the bottom.
private void LoadTable()
{
if (Budgets.Count > 0)
{
// 1- Create the data tableBudgetTable = null; BudgetTable = new DataTable(); // Add the first column DataColumn column = new DataColumn(); column.ColumnName = "Type"; column.DataType = System.Type.GetType("System.String"); column.ReadOnly = false; BudgetTable.Columns.Add(column); // Get a distinct list of Plan/Elevations List<string> planElevations = new List<string>(); switch (BudgetType) { case BudgetType.Job: var sheets = JobSequenceSheets.Where(x => x.Status == "New").ToList(); planElevations = sheets.Select(x => $"{x.Plan}{x.Elevation}").ToList(); break; case BudgetType.Project: planElevations = ProjectPlanTypeSummaries.Select(x => $"{x.Plan}{x.Elevation}").ToList(); break; default: throw new ArgumentException("Unhandled CASE statement"); break; } // Add a column for every distinct Plan/Elevation foreach (var planElevation in planElevations) { column = new DataColumn(); column.ColumnName = planElevation; column.Caption = planEle
If the way you're doing it works without side-effects, that's the right way to do it. :)
".45 ACP - because shooting twice is just silly" - JSOP, 2010
-----
You can never have too much ammo - unless you're swimming, or on fire. - JSOP, 2010
-----
When you pry the gun from my cold dead hands, be careful - the barrel will be very hot. - JSOP, 2013