简体   繁体   中英

XAML Int to boolean converter

I need to create MultiDataTrigger with condition to check if my ObservableCollection Count property <> 0.

So I want to convert Count value of my collection to boolean.

How can I do that?

Something like:

<Condition Binding="{Binding Path=MyCollection.Count, Converter=Int32CollectionConverter, ConverterParameter=<what should be there?>}" Value="true" />

What you are asking for is on the right track, I have exactly that sort of converter myself. Here is an example of how I use it (I've sanitized some of the code):

<Style x:Key="MenuItemStyle" TargetType="{x:Type MenuItem}" d:DataContext="{d:DesignInstance mynamespace:myDataObject, d:IsDesignTimeCreatable=False}">
    <Style.Triggers>
        <DataTrigger Binding="{Binding   
                               Path=ChildMenuItems.Count,   
                               Converter={mynamespace:NumericComparisonToBoolConverter   
                                          ComparisonNumber=0,  
                                          ComparisonType=IsGreaterThan
                                          }}"  
                     Value="true" 
                     >
            <Setter Property="Padding" Value="3,1,7,1"/>
            <Setter Property="Template" Value="{StaticResource someRandomTemplate}"/>
        </DataTrigger>
    </Style.Triggers>
</Style>

This sets a couple of properties on a top level menu item if it has child menu items.

I'm going to leave the guts of the converter as an exercise for you, but here is a rough outline to get you started:

public class NumericComparisonToBoolConverter : MarkupExtension, IValueConverter
{

    public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        if (value != null && IsNumericType(value.GetType()))
        {
            var d = System.Convert.ToDouble(value);

            switch (ComparisonType)
            {
                case NumericComparisonType.IsEqualTo:
                    return ComparisonNumber == d;

                ...etc...
            }
        }

        return false;
    }

    public double ComparisonNumber { get; set; }

    public NumericComparisonType ComparisonType { get; set; }

    public enum NumericComparisonType
    {
        None = 0,
        IsEqualTo,
        IsNotEqualTo,
        IsLessThan,
        IsGreaterThan,
        ...etc...
    }

    protected bool IsNumericType(Type type)
    {
        if (type == null)
            return false;
        switch (Type.GetTypeCode(type))
        {
            case TypeCode.Byte:
            case TypeCode.Decimal:
            case TypeCode.Double:
            ...etc...
                return true;
            case TypeCode.Object:
                if (type.IsGenericType && type.GetGenericTypeDefinition() == typeof(Nullable<>))
                {
                    return IsNumericType(Nullable.GetUnderlyingType(type));
                }
                return false;
        }
        return false;
    }

    public virtual object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        throw new NotImplementedException();
    }

    public override object ProvideValue(IServiceProvider serviceProvider)
    {
        return this;
    }

}

Create a MultiValue converter:

public class MyMultivalueConverter : IMultiValueConverter
{

    public object Convert(object[] values, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        //write your conversion code here
        //values[0] will be the count of your collection as that is first supplied value in MultiBinding
        return true;
    }

    public object[] ConvertBack(object value, Type[] targetTypes, object parameter, System.Globalization.CultureInfo culture)
    {
        throw new NotImplementedException();
    }
}

And use that converter in MultiBinding in XMAL:

 <local:MyMultivalueConverter x:Key="Converter" />
    <MultiBinding Converter="{StaticResource Converter}">
        <Binding RelativeSource="{RelativeSource AncestorType={x:Type YourItemControl}}" Path="dataContext.Collection.Count"/>
        .........
        other conditions
    </MultiBinding>

This Multivalue Converter is returning a bool , so whatever you want as a condition pass it on to converter and it will give you a bool value back.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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