DataGridCell Style Problem
-
I'm creating a WPF DataGrid. The data source is a DataTable created in the VM. [See this pic](https://1drv.ms/u/s!AjBmoYAYz\_v2hBJl1Yj0BBLCyDbW?e=gcMT3H). I'm creating a DataTable in the VM, then binding the DataGrid to it. That part all works fine. I now want to apply a cell style: View Model
string[] columnNames = (from dc in BudgetTable.Columns.Cast()
select dc.ColumnName).ToArray();// Go through each column
foreach (string item in columnNames)
{
//Creating binding
Binding binding = new Binding(item);
string styleName = "";if (item == "Type") { binding.Mode = BindingMode.OneTime; styleName = "budgetGridCellStyle"; } else { binding.Mode = BindingMode.TwoWay; binding.UpdateSourceTrigger = UpdateSourceTrigger.PropertyChanged; styleName = "dataGridCurrencyCellStyle"; } var style = Application.Current.FindResource(styleName) as Style; // Bind the column var col = new DataGridTextColumn() { Header = item, Binding = binding, Visibility = Visibility.Visible, CellStyle = style }; DataGridColumns.Add(col);
}
Style
<Style x:Key="dataGridCurrencyCellStyle"
BasedOn="{StaticResource dataGridCellStyle}"
TargetType="{x:Type DataGridCell}"><Setter Property="TextBlock.Text" Value="{Binding StringFormat={}{0:0.##}}"/> <Setter Property="IsEnabled" Value="{Binding AreFieldsEnabled}"/>
</Style>
The setters won't work: 1. The string format setting is not being applied no matter what I change it to. What's the right way to do this? 2. The IsEnabled causes a BindingExpression error. AreFieldsEnabled is a bool property on the VM. How do I reference that from this style? Thanks
If it's not broken, fix it until it is. Everything makes sense in someone's mind. Ya can't fix stupid.