开发者

wpf DataGridTextColumn refactoring

开发者 https://www.devze.com 2022-12-12 18:28 出处:网络
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.

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 t开发者_开发技巧his style to in the xaml for the DataGridTextColumn?


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...

0

精彩评论

暂无评论...
验证码 换一张
取 消