Reputation: 31
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?
Upvotes: 0
Views: 46
Reputation: 6643
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 targetType
is System.String
. Your targetType
is always System.Object
because you utilized it on a BindingCondition
.
If you want to point out the type manually, you could try ConverterParameter
:
<BindingCondition Binding="{Binding Source={x:Reference retrievingActivity}, Path=IsRunning, Converter={StaticResource boolInvert}, ConverterParameter={x:Type x:Boolean}}" Value="true" />
Then retrieve it in IValueConverter
class like:
try
{
if ((Type)parameter != typeof(bool))
throw new InvalidOperationException("The target must be a boolean");
}
catch (Exception ex)
{
int x = 1;
}
return !(bool)value;
Moreover, we used the if (value is bool)
directly as what Jason said.
Upvotes: 0