tags:

views:

332

answers:

1

Hello

I am using a DataGrid in a WPF app that has several (literally one for each day of the week) columns which differ only in their data index. A sample of the xaml is below.

How can I refactor this into something more DRY?

Cheers,
Berryl

SAMPLE XAML (two of seven columns):

<dg:DataGridTextColumn 
    Header="{Binding Source={StaticResource spy}, Path=DataContext[0].EventDate,   Converter={StaticResource dateConv}}" 
    CellStyle="{StaticResource DataEntryCellStyle}" Width="60" CanUserResize="False"
    Binding="{Binding Allocations[0].Amount, Converter={StaticResource amtConv}}"  
                               />
<dg:DataGridTextColumn 
    Header="{Binding Source={StaticResource spy}, Path=DataContext[1].EventDate, Converter={StaticResource dateConv}}" 
    CellStyle="{StaticResource DataEntryCellStyle}" Width="60" CanUserResize="False"
    Binding="{Binding Allocations[1].Amount, Converter={StaticResource amtConv}}"  
                               />

== EDITED ADD'L INFO @ JALFP ===

Compiler complains that the target type is not a framework element when building

<Style x:Key="dayOfWeekColumn" TargetType="dg:DataGridTextColumn" >
    <Setter Property="CanUserResize" Value="False"/>
    <Setter Property="CanUserSort" Value="False"/>
    <Setter Property="Width" Value="60" />
    <Setter Property="CellStyle" Value="{StaticResource dataEntryGridCellStyle}" />
</Style>

I don't see anything that looks like a ColumnStyle either. What property would I set this style to in the xaml for the DataGridTextColumn?

+1  A: 

Maybe you can create your own class which inherits from DataGridTextColumn and add a new DependencyProperty DayIndex (from 0 to 6). Then in this class you could to the initialization you're doing in the XAML...

But I'm not sure it will be a really better and more maintainable solution...

Jalfp
Hi Jalfp, and thanks for the reply. This makes sense, although I was hoping some clever use of styles and other WPF techniques I am not familiar with yet might be easier.
Berryl
You can use a Style which targets The DataGridTextColumn type and set CellStyle, Width and CanUserResize property.
Jalfp
Hi Jalfp. I tried this without success; added code and comments in edit of the original posting. Can you please take a look? Cheers!
Berryl