wpf - DynamicResource 绑定(bind)到 Color 不起作用

标签 wpf xaml binding styles dynamicresource

我在 xaml 中为我的窗口创建了一个样式,其中包含到 DynamicResource 的绑定(bind):

<Window.Resources>
    <local:RowColorConverter x:Key="RowColorConverter" />
        <Style x:Key="OddEvenRowStyle">
            <Setter Property="DataGridRow.Background">
                <Setter.Value>
                    <Binding RelativeSource="{RelativeSource AncestorType=GroupItem}" Path="(ItemsControl.AlternationIndex)" Converter="{StaticResource RowColorConverter}">
                        <Binding.ConverterParameter>
                            <x:Array Type="Brush">
                                <SolidColorBrush Color="{DynamicResource RowPrimaryBrush}" />
                                <SolidColorBrush Color="{DynamicResource RowSecondaryBrush}" />
                            </x:Array>
                        </Binding.ConverterParameter>
                    </Binding>
                </Setter.Value>
            </Setter>
        </Style>
</Window.Resources>

然后我将样式分配给 DataGrid 的 RowStyle:

<DataGrid Name="dataGrid" AutoGenerateColumns="False" Height="Auto" Width="Auto" ItemsSource="{Binding}" RowStyle="{StaticResource OddEvenRowStyle}">

在我的窗口初始化中,我分配了这些 DynamicResource 值:

Resources["RowPrimaryBrush"] = Colors.LightGray;
Resources["RowSecondaryBrush"] = Colors.DarkGray;

但是,当我加载窗口时,颜色无法正常工作:

enter image description here

我很确定我的其余代码没问题,因为当我将 xaml 中的颜色值更改为颜色值时:

<x:Array Type="Brush">
    <SolidColorBrush Color="LightGray" />
    <SolidColorBrush Color="DarkGray" />
</x:Array>

颜色被正确分配:

enter image description here

这就是为什么我被引导相信它与绑定(bind)有关。我绑定(bind)颜色的方式有问题吗?

最佳答案

Binding.ConverterParameter 不是 WPF 逻辑树的一部分,因此无法在其中执行动态资源查找。

关于wpf - DynamicResource 绑定(bind)到 Color 不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11191466/

相关文章:

c# - XAML 无法从矩形中删除边框

c# - 当 ItemSource 是一个 Observable 集合时,隐藏 DataGrid 中的列

c# - 在 RichEditBox UWP 中显示行号

.net - 帮助选择绑定(bind)(WCF)

c# - WPF - 具有 MultiBinding 和不同 DataContexts 的自定义工具提示

c# - 绑定(bind)多功能 WPF DataGrid

SwiftUI:绑定(bind)从环境对象派生的结构的属性

c# - 绑定(bind) - 回调被调用两次

c# - 是否有用于将 WPF FlowDocument 转换为 WordML 的开源 XSLT?

WPF 数据网格 : Get column binding property for filtering