[英]Access DataGrid.RowStyle in AttachedProperty
XAML: 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>
AttachedProperty: AttachedProperty:
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?");
}
}
Problem : Assert is getting triggered - WHY ? 问题 :断言被触发-为什么?
This is probably the order in which the properties are being set on the DataGrid
. 这可能是在DataGrid
上设置属性的顺序。
In general (I don't know of any exceptions, but I don't want to claim there aren't any) properties are set in the order that they're defined in XAML. 在一般(我不知道任何异常,但我不希望任何权利要求有没有 )属性,它们在XAML正在定义的顺序设置。 So your DataGridExtensions.CanExportToExcel
will be set to True
before DataGrid.RowStyle
is set. 所以,你的DataGridExtensions.CanExportToExcel
将被设置为True
之前DataGrid.RowStyle
设置。
You can test this by removing your current call to uiwpf:DataGridExtensions.CanExportToExcel="True"
, and putting: 您可以通过删除当前对uiwpf:DataGridExtensions.CanExportToExcel="True"
调用并放入以下命令来进行测试:
<uiwpf:DataGridExtensions.CanExportToExcel>True</uiwpf:DataGridExtensions.CanExportToExcel>
After you set <DataGrid.RowStyle>
. 当您设置<DataGrid.RowStyle>
To make your attached property robust, you will probably need to use CanExportToExcelChanged
to set a binding on the RowStyle
property (and remove it again when CanExportToExcel
is set to False
). 为了使附加属性更稳定,您可能需要使用CanExportToExcelChanged
在RowStyle
属性上设置绑定(并在CanExportToExcel
设置为False
时再次将其删除)。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.