简体   繁体   English

MultiTrigger和Converter令人困惑的问题

[英]Perplexing issue with MultiTrigger and Converter

I have the following converter: 我有以下转换器:

public class InverseBooleanConverter : IValueConverter { #region IValueConverter Members 公共类InverseBooleanConverter:IValueConverter {#region IValueConverter成员

    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: 我试图像这样使用它来控制基于代码隐藏属性“CanShowResults”的列表视图的IsVisible,以及页面上的活动指示器:

        <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. 我在Convert方法中遇到异常。 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. targetType用于指出要转换值的类型。 And there's no need to pass it to your IValueConverter class. 而且没有必要将它传递给你的IValueConverter类。 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 . 例如,如果在Lable的Text上使用IValueConverter,则targetTypeSystem.String Your targetType is always System.Object because you utilized it on a BindingCondition . 您的targetType始终是System.Object因为您在BindingCondition上使用它。

If you want to point out the type manually, you could try ConverterParameter : 如果要手动指出类型,可以尝试使用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: 然后在IValueConverter类中检索它,如:

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. 而且,我们直接使用if (value is bool)作为Jason所说的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM