按背景检索 WPF DataGrid 列的列表 属性 Setter

Retrieve List of WPF DataGrid Columns By Background Property Setter

我正在尝试检索 DataGrid 中具有特定背景的列列表 color/brush,使用以下方法:

Dim OutputCols As New List(Of DataGridColumn)
OutputCols = datagrid_Output.Columns.Where(Function(a) Not (a.CellStyle Is Nothing) AndAlso a.GetValue(BackgroundProperty) Is GridOutputsColor).ToList

但是,这不起作用,我发现 GetValue(BackgroundProperty) 正在返回空值。但是,如果我这样做,我会得到 SetterBase 对象,而不是我想要的 Setter(s):

Dim OutputCols As New List(Of DataGridColumn)
OutputCols = datagrid_Output.Columns.Where(Function(a) Not (a.CellStyle Is Nothing) AndAlso a.CellStyle.Setters.First(Function(b) b.property Is BackgroundProperty).value Is GridOutputsColor).ToList

...它不起作用,因为 Function(b) returns SetterBase 对象而不是每个 Setter.

找到讨厌的解决方案:

Dim OutputCols As New List(Of DataGridColumn)
OutputCols = datagrid_Output.Columns.Where(Function(a) Not (a.CellStyle Is Nothing) AndAlso TryCast(a.CellStyle.Setters.Where(Function(b) TryCast(b, Setter).Property Is BackgroundProperty).ToList.First, Setter).Value Is GridOutputsColor).ToList