hello I've tried to multibind of rectangle.
but i've got System.InvalidCastException: ‘Unable to cast object of type ‘MS.Internal.NamedObject’ to type ‘System.Boolean’.’ error
public object Convert(object[] values, Type targetType, object parameter, CultureInfo culture)
{
SolidColorBrush color = new SolidColorBrush(Colors.Transparent);
if (values == null)
{
return color;
}
bool A= (bool)(values[0]);
bool B= (bool)values[1];
if (A&& B)
{
color = new SolidColorBrush(Colors.Red);
}
else if (A== true && B== false)
{
color = new SolidColorBrush(Colors.Gray);
}
else if (A== true && B== true)
{
color = new SolidColorBrush(Colors.Green);
}
else
{
color = new SolidColorBrush(Colors.LightBlue);
}
return color;
}
public object[] ConvertBack(object value, Type[] targetTypes, object parameter, CultureInfo culture)
{
throw new NotImplementedException();
}
}
I Want to change colors of rectangle depending on conditions.
xaml
<Rectangle.Fill>
<MultiBinding Converter="{StaticResource BackGroundColorConverters}">
<Binding Path="CheckAStatus"/>
<Binding Path="CheckDStatus"/>
</MultiBinding>
</Rectangle.Fill>
</Rectangle>
private bool _checkAStatus; public bool CheckAStatus { get => _checkAStatus; set => SetProperty(ref _checkAStatus, value);
}
private bool _checkDStatus; public bool CheckDStatus { get => _checkDStatus; set => SetProperty(ref _checkDStatus, value); } public ViewModel() { this.CheckAstatus = false; this.CheckDstats = true; }
CodePudding user response:
You should validate the arguments in your Convert
method and do nothing when an MS.Internal.NamedObject
is passed as an argument:
public object Convert(object[] values, Type targetType, object parameter, CultureInfo culture)
{
if (values == null || values.Length < 2 || !(values[0] is bool A) || !(values[1] is bool B)
return Binding.DoNothing;
SolidColorBrush color = new SolidColorBrush(Colors.Transparent);
if (values == null)
{
return color;
}
if (A && B)
{
color = new SolidColorBrush(Colors.Red);
}
else if (A == true && B == false)
{
color = new SolidColorBrush(Colors.Gray);
}
else if (A == true && B == true)
{
color = new SolidColorBrush(Colors.Green);
}
else
{
color = new SolidColorBrush(Colors.LightBlue);
}
return color;
}