I was browsing stackoverflow to try to figure out a way to style the GridViewColumns in my ListView. I came across this:
WPF Text Formatting in GridViewColumn
The top answer shows how to style one (1) GridViewColumn. My question is, is there a way to style these GridViewColumns in the Window.Resources so I don't have to do it for each individual GridViewColumn?
There are a few possibilities:
<!-- if style doesn't change -->
<GridViewColumn CellTemplate="{StaticResource yourCellTemplate}"/>
<!-- if you need to change it up based on criteria - template selector-->
<GridViewColumn CellTemplateSelector="{StaticResource YourTemplateSelector}"/>
<!-- same goes for headers -->
<GridViewColumn HeaderTemplate="{StaticResource yourheaderTempalte}"/>
..or HeaderContainerStyle, HeaderTemplateSelector
if you want to use template selectors: create a class, instanciate it in resource dict, and plug it in you gridview column, here's a little sample
public class MyTemplateSelector : DataTemplateSelector
{
public DataTemplate SimpleTemplate { get; set; }
public DataTemplate ComplexTemplate { get; set; }
public override DataTemplate SelectTemplate(object item, DependencyObject container)
{
//if I have just text
return SimpleTemplate;
//if I have comments and other fancy stuff
return ComplexTemplate;
then in your ResourceDictionary
<DataTemplate x:Key="ComplexTemplate">
<Views:MyCustomControl DataContext="{Binding}"/>
</DataTemplate>
<Views:MyTemplateSelector
x:Key="TxtVsExpensiveCell_TemplateSelector"
SimpleTemplate ="{StaticResource SimpleTemplate}"
ComplexTemplate="{StaticResource ComplexTemplate}"/>
<!-- then you use it in your view like this -->
<GridViewColumn CellTemplateSelector="{StaticResource TxtVsExpensiveCell_TemplateSelector}"/>
If you don't want to go through all that trouble, and just what to tweak styles of predefined controls, why not use DataGrid? It has predefined columns, where you can tweak styles per each..
<DataGrid>
<DataGrid.Columns>
<DataGridTextColumn/>
<DataGridCheckBoxColumn/>
<DataGridHyperlinkColumn/>
<DataGridCheckBoxColumn/>
....there are several more column types!
</DataGrid.Columns>
</DataGrid>