I have the following converter:
public class InverseBooleanConverter : IValueConverter { #region IValueConverter Members
public object Convert(object value, Type targetType, object parameter,
System.Globalization.CultureInfo culture)
{
try
{
if (targetType != typeof(bool))
throw new InvalidOperationException("The target must be a boolean");
}
catch(Exception ex)
{
int x = 1;
}
return !(bool)value;
}
public object ConvertBack(object value, Type targetType, object parameter,
System.Globalization.CultureInfo culture)
{
throw new NotSupportedException();
}
#endregion
}
and I'm attempting to use it like this, to control IsVisible of a listview based on a code-behind property "CanShowResults", and an activity indicator on the page:
<ListView x:Name="listView" BackgroundColor="White" SeparatorColor="#e0e0e0" IsVisible="False">
<ListView.Triggers>
<MultiTrigger TargetType="ListView">
<MultiTrigger.Conditions>
<BindingCondition Binding="{Binding Source={x:Reference retrievingActivity}, Path=IsRunning, Converter={StaticResource boolInvert}}" Value="true" />
<BindingCondition Binding="{Binding Path=CanShowResults}" Value="True" />
</MultiTrigger.Conditions>
<Setter Property="IsVisible" Value="True" />
</MultiTrigger>
</ListView.Triggers>
<ListView.ItemTemplate>
. . . . .
I'm getting an exception in the Convert method. I've scoured documentation, does anyone see what I'm doing wrong?
targetType is used for pointing out the type to which to convert the value. And there's no need to pass it to your IValueConverter class. It is automatically set depending on what type it wants to convert it to be.
For example, if you consuming the IValueConverter on a Lable's Text the
targetTypeisSystem.String. YourtargetTypeis alwaysSystem.Objectbecause you utilized it on aBindingCondition.If you want to point out the type manually, you could try
ConverterParameter:Then retrieve it in
IValueConverterclass like:Moreover, we used the
if (value is bool)directly as what Jason said.