How do I bind something like <Color x:Key="SomeColor" /> to a dependency property with Caliburn Micro?
I need to be able to change Color at run-time and have it immediately updated in all things that use it.
Solution:
XAML in SomeClassView.xaml
<SomeControl.Resources>
<SolidColorBrush x:Key="ControlBrush" />
</SomeControl.Resources>
C# in SomeClassViewModel.cs
[Export(typeof(MainWindowViewModel))]
public class MainWindowViewModel : PropertyChangedBase
{
private SolidColorBrush _controlBrush;
public SolidColorBrush ControlBrush
{
get { return _controlBrush; }
set
{
_controlBrush = value;
NotifyOfPropertyChange(() => ControlBrush);
}
}
}
The problem was exactly what Charleh said, I just totally forgot that not everything in WPF can be a DependencyProperty.
You can’t usually directly bind a
Colorobject, you need to use aSolidColorBrush(for solid colour), as that’s what most UI objects expect.e.g.
TextBox.BackgroundexpectsBrush, of whichSolidColorBrushis a subclass of. There are other types of brushes that produce different fills such asLinearGradientBrushHave a look here:
How can I bind a background color in WPF/XAML?
Can you provide some screenshots of what you expect and the XAML?
Edit:
Ok well what you want is pretty simple to achieve, not really related to Caliburn.Micro at all 🙂
Create your styles as usual, but bind the brushes
Colorproperty dynamically usingDynamicResource. When you update the colour itself, the resource binding will be evaluated again and the colours will change.Example XAML:
Code-behind: