Reputation: 85
There is a border inside a itemtemplate of a list view like this:
<ListView.ItemTemplate>
<DataTemplate>
<Border >
<Border.Background>
<SolidColorBrush Color="{Binding Path=IsTrue, Converter={StaticResource ResourceKey=ColorConventer}}" Opacity="0.2">
</SolidColorBrush>
</Border.Background>
</Border>
bool to color converter:
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
try
{
if (value != null)
{
switch ((bool)value)
{
case true:
//return System.Windows.Media.Brushes.Red;
return new SolidColorBrush(Colors.Red);
case false:
return new SolidColorBrush(Colors.White);
}
}
}
catch { }
return value;
}
The ColorConventer is called but color of SolidColorBrush is not changed. can anyone help that what is the reason?
Upvotes: 1
Views: 1108
Reputation: 128136
You can't bind the Color
property of a SolidColorBrush to another SolidColorBrush. Change your converter so that it returns a Color:
public object Convert(
object value, Type targetType, object parameter, CultureInfo culture)
{
if (value is bool)
{
return (bool)value ? Colors.Red : Colors.White;
}
return value;
}
With your original converter you could have written the Background Binding like this:
<Border Background="{Binding Path=IsTrue, Converter={StaticResource ColorConventer}}" />
But the SolidColorBrush returned from the converter would have to have its Opacity set, e.g. like this:
public object Convert(
object value, Type targetType, object parameter, CultureInfo culture)
{
if (!(value is bool))
{
return value;
}
return new SolidColorBrush
{
Color = (bool)value ? Colors.Red : Colors.White,
Opacity = 0.2
};
}
Upvotes: 2