Without too much workarounds you can define a list of brushes in XAML as StaticResource
:
<Window.Resources>
<x:Array Type="{x:Type SolidColorBrush}" x:Key="ButtonColor">
<SolidColorBrush Color="Red"></SolidColorBrush>
<SolidColorBrush Color="Blue"></SolidColorBrush>
<SolidColorBrush Color="Green"></SolidColorBrush>
</x:Array>
</Window.Resources>
<StackPanel>
<Button Background="{Binding Source={StaticResource ButtonColor}, Path=[0]}" Content="First Button"/>
<Button Background="{Binding Source={StaticResource ButtonColor}, Path=[1]}" Content="Second Button"/>
<Button Background="{Binding Source={StaticResource ButtonColor}, Path=[2]}" Content="Third Button"/>
</StackPanel>
For DynamicResource
, you have to use a workaround as found here:
<Window.Resources>
<x:Array Type="{x:Type SolidColorBrush}" x:Key="ButtonColor">
<SolidColorBrush Color="Red"></SolidColorBrush>
<SolidColorBrush Color="Blue"></SolidColorBrush>
<SolidColorBrush Color="Green"></SolidColorBrush>
</x:Array>
</Window.Resources>
<StackPanel>
<Button DataContext="{DynamicResource ButtonColor}" Background="{Binding [0]}" Content="First Button"/>
<Button DataContext="{DynamicResource ButtonColor}" Background="{Binding [1]}" Content="Second Button"/>
<Button DataContext="{DynamicResource ButtonColor}" Background="{Binding [2]}" Content="Third Button"/>
</StackPanel>
This method prevents you from using the DataContext
property as you would normally.
Now, if you want to change one of the Background using a command, your command code would be:
private void ChangeToBlueBackgroundExecute(object parameter) {
((SolidColorBrush[])Resources["ButtonColor"])[(int)parameter].Color = Colors.Blue;
}
For more details on the XAML path syntax, read more here.