XAML Int到布尔转换器

时间:2016-02-09 10:09:34

标签: c# wpf xaml

我需要创建带条件的MultiDataTrigger来检查我的ObservableCollection Count属性<> 0

所以我想将我的集合的Count值转换为boolean。

我该怎么做?

类似的东西:

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

2 个答案:

答案 0 :(得分:3)

你要求的是在正确的轨道上,我自己就是那种转换器。这是我如何使用它的一个例子(我已经清理了一些代码):

<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>

如果它具有子菜单项,则会在顶级菜单项上设置几个属性。

我将把转换器的内容作为练习留给你,但这是一个粗略的大纲,让你开始:

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;
    }

}

答案 1 :(得分:0)

创建MultiValue转化器:

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();
    }
}

在XMAL的MultiBinding中使用该转换器:

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

这个多值转换器正在返回一个 bool ,所以无论你想要什么条件都将它传递给转换器,它会给你一个bool值。