We had a similar situation when we needed another Path property for Binding , but otherwise similar to CellStyle for each DataGridColumn . We solved this with a custom MarkupExtension . In your case, it will look like
<tk:DataGrid AutoGenerateColumns="False" ItemsSource="{Binding MyItems}"> <tk:DataGrid.Columns> <tk:DataGridTextColumn Header="A" Binding="{Binding colA}" /> <tk:DataGridTextColumn Header="B" Binding="{Binding colB, StringFormat=\{0:P\}}" CellStyle="{markup:ForegroundCellStyle PropertyName=colB}"/> <tk:DataGridTextColumn Header="C" Binding="{Binding colC, StringFormat=\{0:P\}}" CellStyle="{markup:ForegroundCellStyle PropertyName=colC}"/> <tk:DataGridTextColumn Header="D" Binding="{Binding colD, StringFormat=\{0:P\}}" CellStyle="{markup:ForegroundCellStyle PropertyName=colD}"/> </tk:DataGrid.Columns> </tk:DataGrid>
and then ForegroundCellStyleExtension creates a Style for the DataGridCell depending on the PropertyName
ForegroundCellStyleExtension
public class ForegroundCellStyleExtension : MarkupExtension { public ForegroundCellStyleExtension() { } public ForegroundCellStyleExtension(string propertyName) { PropertyName = propertyName; } public string PropertyName { get; set; } public override object ProvideValue(IServiceProvider serviceProvider) { IProvideValueTarget service = (IProvideValueTarget)serviceProvider.GetService(typeof(IProvideValueTarget)); DependencyObject targetObject = service.TargetObject as DependencyObject; if (targetObject == null) { return null; } Binding foregroundBinding = new Binding { Path = new PropertyPath(PropertyName), Converter = new ValueConverter() }; Style foregroundCellStyle = new Style(typeof(DataGridCell)); foregroundCellStyle.Setters.Add(new Setter(DataGridCell.ForegroundProperty, foregroundBinding)); return foregroundCellStyle; } }
In addition, if you have other Setters , etc. that you would like to use, they can be included with another parameter in MarkupExtension .
<Window.Resources> <Style x:Key="dataGridCellStyle" TargetType="{x:Type tk:DataGridCell}"> <Setter Property="Background" Value="Blue"/> </Style> </Window.Resources> <tk:DataGridTextColumn Header="B" Binding="{Binding colB, StringFormat=\{0:P\}}" CellStyle="{markup:ForegroundCellStyle colB, {StaticResource dataGridCellStyle}}"/>
And ForegroundCellStyleExtension will use the second parameter as BasedOn for the DataGridCell Style
ForegroundCellStyleExtension with BasedOn
public class ForegroundCellStyleExtension : MarkupExtension { public ForegroundCellStyleExtension() { } public ForegroundCellStyleExtension(string propertyName, Style basedOnCellStyle) { PropertyName = propertyName; BasedOnCellStyle = basedOnCellStyle; } public string PropertyName { get; set; } public Style BasedOnCellStyle { get; set; } public override object ProvideValue(IServiceProvider serviceProvider) { IProvideValueTarget service = (IProvideValueTarget)serviceProvider.GetService(typeof(IProvideValueTarget)); DependencyObject targetObject = service.TargetObject as DependencyObject; if (targetObject == null) { return null; } Binding foregroundBinding = new Binding { Path = new PropertyPath(PropertyName), Converter = new ValueConverter() }; Style foregroundCellStyle = new Style(typeof(DataGridCell), BasedOnCellStyle); foregroundCellStyle.Setters.Add(new Setter(DataGridCell.ForegroundProperty, foregroundBinding)); return foregroundCellStyle; } }
Fredrik hedblad
source share