A
A
Abc Edc2016-05-12 16:59:13
Angular
Abc Edc, 2016-05-12 16:59:13

How to make multiple converters in xaml?

In Angular, you can make several filters over text, for example

<div ng-bind="vm.value | filter1| filter2| filter3"

And the sequence is preserved.
How to do this in XAML? in particular in xmarine, but there is nothing different here, as I understand it, from just xaml there is no
<Label Font="20"  Text="{Binding MovingState.HashRate,Converter={StaticResource FloatConverter...

For example, I would like to first double convert to a string, then do something with the string even before the heap, and even better so that the parameters can be transferred unlimitedly, otherwise I immediately spawned these converters

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Alexey Pavlov, 2016-05-12
@gleber1

In your case, the creation of a special custom converter is suitable, which will make it from a number to a string, and then process the string.
Another option is MultiBinding. A special multi-converter is created, in which several ordinary bindings are indicated. But they are processed in parallel, so if you first need to make the first converter, and then use its result in another converter, then this will not work, all data is processed in a multiconverter.
But multibinding works in WPF, but not in Silverlight. But there is a workaround for Silverlight: www.codeproject.com/Articles/286171/MultiBinding-i...
Here is an example of a multi-converter that takes multiple bools and checks if they are all true (or false - you can choose).
Converter usage:

xmlns:converters="clr-namespace:MyProject.Converters"

<CheckBox x:Name="Checkbox1" Content="Чекбокс 1"/>
<CheckBox x:Name="Checkbox2" Content="Чекбокс 2"/>
<ToggleButton Content="Кнопка">
    <ToggleButton.IsEnabled>
        <MultiBinding Converter="{converters:BooleanMultiConverter Type=AllTrue}">
            <Binding Path="IsChecked" ElementName="Checkbox1"/>
            <Binding Path="IsChecked" ElementName="Checkbox2"/>
        </MultiBinding>
    </ToggleButton.IsEnabled>
</ToggleButton>

The converter itself:
using System;
using System.Linq;
using System.Windows;
using System.Windows.Data;
using System.Windows.Markup;

namespace MyProject.Converters
{
    public class BooleanMultiConverter : MarkupExtension, IMultiValueConverter
    {
        /// <summary>
        /// Перечисление типа конвертера
        /// </summary>
        public enum Types
        {
            AllTrue,
            AllFalse,
            AnyTrue,
            AnyFalse,
        }

        /// <summary>
        /// Тип конвертера, по умолчанию - AllTrue
        /// </summary>
        public Types Type { get; set; }

        public object Convert(object[] values, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            switch (Type)
            {
                case Types.AllTrue:
                    return values.All(v => (bool)v);
                case Types.AllFalse:
                    return values.All(v => !(bool)v);
                case Types.AnyTrue:
                    return values.Any(v => (bool)v);
                case Types.AnyFalse:
                    return values.Any(v => !(bool)v);
                default:
                    throw new InvalidOperationException();
            }
        }

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

        #region MarkupExtension

        public BooleanMultiConverter()
        {
        }

        /// <summary>
        /// Отображать конвертер в списке
        /// </summary>
        public override object ProvideValue(IServiceProvider serviceProvider)
        {
            return this;
        }

        #endregion
    }
}

MarkupExtension is used to avoid creating a bunch of converters in resources

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question