Posted by: Zeeshan Amjad | November 26, 2013

Logical Multivalue Converter with LINQ


We just studied the logical multivalve converter here. Now we are going to do the same thing but instead of doing it manually traversing every values and apply the “AND” or “OR” logical operation, we can achieve the same thing with LINQ. We can use the Extension methods “All” and “Any” defined for IEnumerable type to do the same thing.

Here is an updated version of our Logical And converter.

Code Snippet
using System.Linq;

namespace MyConverter
{
    using System.Windows.Data;

    [ValueConversion(typeof(bool), typeof(bool))]
    public class AndConverter : IMultiValueConverter
    {
        public object Convert(object[] values, System.Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            return values.Cast<bool>().All(value => value);
        }

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

 

Similarly here is an updated version of Logical Or operator.

Code Snippet
using System.Linq;

namespace MyConverter
{
    using System.Windows.Data;

    [ValueConversion(typeof(bool), typeof(bool))]
    public class OrConverter : IMultiValueConverter
    {
        public object Convert(object[] values, System.Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            return values.Cast<bool>().Any(value => value);
        }

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

 

Rest of the code of this application and out put is exactly the same as previous program. Here is our C# code.

 

Code Snippet
namespace MyConverter
{
    using System.Linq;
    using System.ComponentModel;

    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow
    {
        public MainWindow()
        {
            InitializeComponent();

            DataContext = new MyConditions();
        }
    }

    public class MyConditions : INotifyPropertyChanged
    {
        private bool _condition1;

        #region NotifyPropertyChanged Methods

        public void RaisePropertyChanged(string propertyName)
        {
            var handler = PropertyChanged;

            if (handler != null)
            {
                handler(this, new PropertyChangedEventArgs(propertyName));
            }
        }

        public event PropertyChangedEventHandler PropertyChanged;

        #endregion

        public bool Condition1
        {
            get
            {
                return _condition1;
            }

            set
            {
                _condition1 = value;
                RaisePropertyChanged("Condition1");
            }
        }

        public bool Condition2
        {
            get
            {
                return _condition1;
            }

            set
            {
                _condition1 = value;
                RaisePropertyChanged("Condition2");
            }
        }

        public bool Condition3
        {
            get
            {
                return _condition1;
            }

            set
            {
                _condition1 = value;
                RaisePropertyChanged("Condition3");
            }
        }

        public bool Condition4
        {
            get
            {
                return _condition1;
            }

            set
            {
                _condition1 = value;
                RaisePropertyChanged("Condition4");
            }
        }
    }
}

 

And here is our XAML.

Code Snippet
<Window x:Class="MyConverter.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation&quot;
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml&quot;
        xmlns:local="clr-namespace:MyConverter"
        Title="And Or Converters" Height="300" Width="400">
    <Window.Resources>
        <local:AndConverter x:Key="AndConverter"/>
        <local:OrConverter x:Key="OrConverter"/>
    </Window.Resources>
    <Grid>
        <Grid.ColumnDefinitions>
            <ColumnDefinition/>
            <ColumnDefinition/>
        </Grid.ColumnDefinitions>
        <Grid.RowDefinitions>
            <RowDefinition/>
            <RowDefinition/>
            <RowDefinition/>
            <RowDefinition/>
            <RowDefinition/>
        </Grid.RowDefinitions>
        <CheckBox Grid.Row="0" VerticalAlignment="Center"
                  Name="Condition1" Margin="5" IsChecked="{Binding Condition1}"
                  Content="Conditin 1"/>
        <CheckBox Grid.Row="1" Grid.Column="0" VerticalAlignment="Center"
                  Name="Condition2" Margin="5" IsChecked="{Binding Condition2}"
                  Content="Conditin 2"/>
        <CheckBox Grid.Row="2" Grid.Column="0" VerticalAlignment="Center"
                  Name="Condition3" Margin="5" IsChecked="{Binding Condition3}"
                  Content="Conditin 3"/>
        <CheckBox Grid.Row="3" Grid.Column="0" VerticalAlignment="Center"
                  Name="Condition4" Margin="5" IsChecked="{Binding Condition4}"
                  Content="Conditin 4"/>
        <TextBox Grid.Row="4" Grid.Column="0" VerticalAlignment="Center"
                   Margin="5" Text="And Value Converter">
            <TextBox.IsEnabled>
                <MultiBinding Converter="{StaticResource AndConverter}">
                    <Binding Path="Condition1"/>
                    <Binding Path="Condition2"/>
                    <Binding Path="Condition3"/>
                    <Binding Path="Condition4"/>
                </MultiBinding>
            </TextBox.IsEnabled>
        </TextBox>
        <TextBox Grid.Column="1" Grid.Row="4" VerticalAlignment="Center"
                   Margin="5" Text="Or Value Converter">
            <TextBox.IsEnabled>
                <MultiBinding Converter="{StaticResource OrConverter}">
                    <Binding Path="Condition1"/>
                    <Binding Path="Condition2"/>
                    <Binding Path="Condition3"/>
                    <Binding Path="Condition4"/>
                </MultiBinding>
            </TextBox.IsEnabled>
        </TextBox>
    </Grid>
</Window>

 

Here is an output of the program.

LogicalMultiValueConverter

Advertisements

Responses

  1. Hi. Great article! I never though you can use multi binding this easy with converters. I noticed you wanted to have _condition2, _condition3 and _condition4 instead of _condition1 for Condition property in MyConditions.

    • Thanks to like it and thanks to point out the mistake.
      Best regards
      Zeeshan Amjad


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Categories

%d bloggers like this: