我在WPF应用程序中使用了一个DataGrid,该应用程序具有几个(一般为一周的一周)列,这些列仅在其数据索引中有所不同。 xaml的样本如下:
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.
如何将其重新导入更多的DRY?
How can I refactor this into something more DRY?
干杯, Berryl
Cheers, Berryl
样品XAML(七列中的两列):
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 ===
== 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>我也看不到任何类似ColumnStyle的东西。在DataGridTextColumn的xaml中,我将设置什么属性?
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?
推荐答案也许你可以创建自己的类继承自DataGridTextColumn并添加一个新的DependencyProperty DayIndex(从0到6)。然后在这个课程中,你可以在XAML中进行初始化...
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...
更多推荐
wpf DataGridTextColumn重构
发布评论