I have a MultiValueConverter returning one of two SolidColoBrush passed as values[] elements depending on a bool passed as values[] elment as well.
All values[] elements are DependencyProperty.
The problem is that when I bind a Background property to these SolidColorBrush through this MultiValueConverter i get a cast error.
If I bind the SolidColorBrush.Color property to these SolidColorBrush (which seems an error to me unless there is an implicit conversion somewhere) everything works fine at runtime, but the designer does not show the desired Background, it shows always the background associated with the "false" value of my DependencyProperty even if I set the property to true.
The code is this:
XAML - UserControl
<UserControl.Resources>
<local:State2BackgroundConverter x:Key="state2BackgroundConverter"/>
</UserControl.Resources>
<Grid.Background>
<SolidColorBrush>
<SolidColorBrush.Color>
<MultiBinding Converter="{StaticResource state2BackgroundConverter}">
<Binding RelativeSource="{RelativeSource Mode=FindAncestor, AncestorType={x:Type UserControl}}" Path="Status"/>
<Binding RelativeSource="{RelativeSource Mode=FindAncestor, AncestorType={x:Type UserControl}}" Path="BrushOFF"/>
<Binding RelativeSource="{RelativeSource Mode=FindAncestor, AncestorType={x:Type UserControl}}" Path="BrushON"/>
</MultiBinding>
</SolidColorBrush.Color>
</SolidColorBrush>
</Grid.Background> // <--- This works at runtime but not at design time
<Grid.Background>
<MultiBinding Converter="{StaticResource state2BackgroundConverter}">
<Binding RelativeSource="{RelativeSource Mode=FindAncestor, AncestorType={x:Type UserControl}}" Path="Status"/>
<Binding RelativeSource="{RelativeSource Mode=FindAncestor, AncestorType={x:Type UserControl}}" Path="BrushOFF"/>
<Binding RelativeSource="{RelativeSource Mode=FindAncestor, AncestorType={x:Type UserControl}}" Path="BrushON"/>
</MultiBinding>
</Grid.Background> // <--- This generates a cast error
XAML - Window
<LightControls:MyButton Margin="1,0,0,0" Height="80" HorizontalAlignment="Left" Width="80" BrushON="#FF7CFEFF" BrushOFF="#FF0096FF" Status="True"/>
C#
public class Status2ColorConverter : IMultiValueConverter
{
public object Convert(object[] values, Type targetType, object parameter, CultureInfo culture)
{
if ((bool)values[0] == true)
return ((SolidColorBrush)values[2]);
else
return ((SolidColorBrush)values[1]);
}
public object[] ConvertBack(object value, Type[] targetTypes, object parameter, CultureInfo culture)
{
return (null);
}
}
private static readonly DependencyProperty StatusProperty =
DependencyProperty.Register("Status",
typeof(bool),
typeof(MyButton),
new PropertyMetadata(tre)
);
public bool Status
{
get { return (bool)this.GetValue(StatusProperty); }
set { this.SetValue(StatusProperty, value); }
}
private static readonly DependencyProperty BrushONProperty = DependencyProperty.Register("BrushON",
typeof(SolidColorBrush),
typeof(MyButton),
new PropertyMetadata(new SolidColorBrush(Colors.Cyan))
);
public SolidColorBrush BrushON
{
get { return (SolidColorBrush)this.GetValue(BrushONProperty); }
set { this.SetValue(BrushONProperty, value); }
}
private static readonly DependencyProperty BrushOFFProperty = DependencyProperty.Register("BrushOFF",
typeof(SolidColorBrush),
typeof(MyButton),
new PropertyMetadata(new SolidColorBrush(Colors.Black))
);
public SolidColorBrush BrushOFF
{
get { return (SolidColorBrush)this.GetValue(BrushOFFProperty); }
set { this.SetValue(BrushOFFProperty, value); }
}
So my questions are:
- Why I have to bind SolidColorBrush.Color property even if my BrushON/BrushOFF return SolidColorBrush and not SolidColorBrush.Color?
- How I can get this to work at design time? (NOTE: all the other Dependency Property in my project, including those having a custom ValueConverter, not MultiValueConverter, work just fine at design time, even other Background properties)
Thanks in advance!