XAML:
<DataGrid ItemsSource="{Binding Source={StaticResource Lines}}"
uiwpf:DataGridExtensions.CanExportToExcel="True">
<DataGrid.RowStyle>
<Style TargetType="DataGridRow">
<Setter Property="ContextMenu" Value="{StaticResource RowContextMenu}" />
</Style>
</DataGrid.RowStyle>
...
</DataGrid>
附属物:
private static void CanExportToExcelChanged(
DependencyObject d,
DependencyPropertyChangedEventArgs e)
{
//Just my way of secure casting DependencyObject -> DataGrid
if(d is DataGrid dataGrid)
{
Debug.Assert(dataGrid.RowStyle != null, "Why is this null?");
}
}
问题:断言被触发 - 为什么?
这可能是在DataGrid
上设置属性的顺序。
一般情况下(我不知道任何异常,但我不想声称没有任何异常)属性按照它们在XAML中定义的顺序设置。因此,在设置DataGridExtensions.CanExportToExcel
之前,你的True
将被设置为DataGrid.RowStyle
。
您可以通过删除当前对uiwpf:DataGridExtensions.CanExportToExcel="True"
的调用来测试这一点,并将:
<uiwpf:DataGridExtensions.CanExportToExcel>True</uiwpf:DataGridExtensions.CanExportToExcel>
设置<DataGrid.RowStyle>
后。
为了使附加属性健壮,您可能需要使用CanExportToExcelChanged
在RowStyle
属性上设置绑定(并在CanExportToExcel
设置为False
时再次删除它)。