Reputation: 998
I have a style being created in the xaml for my window which contains bindings to a DynamicResource:
<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>
I then assign the style to a RowStyle for a DataGrid:
<DataGrid Name="dataGrid" AutoGenerateColumns="False" Height="Auto" Width="Auto" ItemsSource="{Binding}" RowStyle="{StaticResource OddEvenRowStyle}">
In the initialization of my window I'm assigning these DynamicResource values:
Resources["RowPrimaryBrush"] = Colors.LightGray;
Resources["RowSecondaryBrush"] = Colors.DarkGray;
However when I load up the window the colors aren't working correctly:
I'm pretty sure the rest of my code is okay because when I change the Color values in xaml to color values:
<x:Array Type="Brush">
<SolidColorBrush Color="LightGray" />
<SolidColorBrush Color="DarkGray" />
</x:Array>
The colors get assigned correctly:
Which is why I'm led to believe it's something to do with the bindings. Is there something wrong with the way I'm binding my colors?
Upvotes: 3
Views: 4000
Reputation: 367
Use a MultiBinding instead of a binding with a converter parameter.
Upvotes: 0
Reputation:
Make sure you set your resources before your window initializes.
public MainWindow()
{
Resources["RowPrimaryBrush"] = Colors.LightGray;
Resources["RowSecondaryBrush"] = Colors.DarkGray;
InitializeComponent();
}
A dynamic resource won't update when changed, like a Binding. Its just deferred until runtime rather than evaluated at compile time.
Not familiar with the limitation, but its not surprising. You might have to rewrite your RowColorConverter to take its argument directly and then update it from codebehind via
(Resources["RowColorConverter"] as RowColorConverter).Parameter =
new Brush[]
{
Brushes.LightGray,
Brushes.DarkGray
}
Or define an attached property within your RowColorConverter
#region Brushes Attached DependencyProperty
public static readonly DependencyProperty BrushesProperty = DependencyProperty.RegisterAttached(
BrushesPropertyName,
typeof(SolidColorBrush[]),
typeof(RowColorConverter),
new FrameworkPropertyMetadata(null)
);
public const string BrushesPropertyName = "Brushes";
public static void SetBrushes(DependencyObject element, SolidColorBrush[] value)
{
element.SetValue(BrushesProperty, value);
}
public static SolidColorBrush[] GetBrushes(DependencyObject element)
{
return (SolidColorBrush[])element.GetValue(BrushesProperty);
}
#endregion
that you can set differently on each grid
<DataGrid Name="dataGrid" SnipPointlessAttributes="True">
<local:RowColorConverter.Colors>
<x:Array Type="Brush">
<SolidColorBrush Color="LightGray" />
<SolidColorBrush Color="DarkGray" />
</x:Array>
</local:RowColorConverter.Colors>
</DataGrid>
Upvotes: 2
Reputation: 178630
Binding.ConverterParameter
is not part of WPF's logical tree, so performing dynamic resource lookups within it won't work.
Upvotes: 3