访问附加属性中的DataGrid.RowStyle

问题描述 投票:1回答:1

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?");
    }
}

问题:断言被触发 - 为什么?

c# wpf xaml attached-properties
1个回答
2
投票

这可能是在DataGrid上设置属性的顺序。

一般情况下(我不知道任何异常,但我不想声称没有任何异常)属性按照它们在XAML中定义的顺序设置。因此,在设置DataGridExtensions.CanExportToExcel之前,你的True将被设置为DataGrid.RowStyle

您可以通过删除当前对uiwpf:DataGridExtensions.CanExportToExcel="True"的调用来测试这一点,并将:

<uiwpf:DataGridExtensions.CanExportToExcel>True</uiwpf:DataGridExtensions.CanExportToExcel>

设置<DataGrid.RowStyle>后。

为了使附加属性健壮,您可能需要使用CanExportToExcelChangedRowStyle属性上设置绑定(并在CanExportToExcel设置为False时再次删除它)。

© www.soinside.com 2019 - 2024. All rights reserved.