IMultiValueConverter and IValueConverter : IValueConverter « Windows Presentation Foundation « C# / CSharp Tutorial

Home
C# / CSharp Tutorial
1.Language Basics
2.Data Type
3.Operator
4.Statement
5.String
6.struct
7.Class
8.Operator Overload
9.delegate
10.Attribute
11.Data Structure
12.Assembly
13.Date Time
14.Development
15.File Directory Stream
16.Preprocessing Directives
17.Regular Expression
18.Generic
19.Reflection
20.Thread
21.I18N Internationalization
22.LINQ
23.GUI Windows Forms
24.Windows Presentation Foundation
25.Windows Communication Foundation
26.Workflow
27.2D
28.Design Patterns
29.Windows
30.XML
31.XML LINQ
32.ADO.Net
33.Network
34.Directory Services
35.Security
36.unsafe
C# / C Sharp
C# / C Sharp by API
C# / CSharp Open Source
C# / CSharp Tutorial » Windows Presentation Foundation » IValueConverter 
24.146.1.IMultiValueConverter and IValueConverter
<Window x:Class="ColorConverter.Window1"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
  xmlns:local="clr-namespace:ColorConverter"
    Title="Colors" Height="300" Width="300">
  <Window.Resources>
    <local:ColorMultiConverter x:Key="colorConverter"/>
    <local:ColorNameValueConverter x:Key="colorNameConverter"/>
  </Window.Resources>
  <Grid>
    <Label Name="label1" Width="39">Red:</Label>
    <Slider Name="redSlider" VerticalAlignment="Top" Maximum="255" />
    <Label Name="label2">Green:</Label>
    <Slider Name="greenSlider" Maximum="255" />
    <Label Name="label3">Blue:</Label>
    <Slider Name="blueSlider" Maximum="255" />
    <Grid Name="colorBlock" >
        <Grid.Background>
          <SolidColorBrush>
            <SolidColorBrush.Color>
              <MultiBinding Converter="{StaticResource colorConverter}">
                <Binding ElementName="redSlider" Path="Value"/>
                <Binding ElementName="greenSlider" Path="Value"/>
                <Binding ElementName="blueSlider" Path="Value"/>
              </MultiBinding>
            </SolidColorBrush.Color>
          </SolidColorBrush>
        </Grid.Background>
        <Label Name="label4" HorizontalContentAlignment="Center" VerticalContentAlignment="Center" >
          <Label.Foreground>
            <SolidColorBrush>
              <SolidColorBrush.Color>
                <MultiBinding Converter="{StaticResource colorConverter}" ConverterParameter="inverse">
                  <Binding ElementName="redSlider" Path="Value"/>
                  <Binding ElementName="greenSlider" Path="Value"/>
                  <Binding ElementName="blueSlider" Path="Value"/>
                </MultiBinding>
              </SolidColorBrush.Color>
            </SolidColorBrush>
          </Label.Foreground>
          <Label.Content>
            <PriorityBinding FallbackValue="Unknown">
              <Binding ElementName="colorBlock" Path="Background.Color" Converter="{StaticResource colorNameConverter}"/>
              <Binding ElementName="colorBlock" Path="Background.Color"/>
            </PriorityBinding>
          </Label.Content>
        </Label>
    </Grid>
  </Grid>
</Window>

//File:Window.xaml.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Collections.Generic;
using System.Windows;
using System.Reflection;


namespace ColorConverter
{
  public partial class Window1 : Window
  {
    public Window1()
    {
      InitializeComponent();
    }

    private void colorBlock_MouseDown(object sender, MouseButtonEventArgs e)
    {
      Random rnd = new Random((int)DateTime.Now.Ticks);

      Color newColor = Color.FromRgb((byte)rnd.Next(255)(byte)rnd.Next(255)(byte)rnd.Next(255));
      colorBlock.Background = new SolidColorBrush(newColor);
    }
  }
  class ColorMultiConverter : IMultiValueConverter
  {

    public object Convert(object[] values, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
      bool inverse = (parameter != null&& (string.Compare(parameter.ToString()"inverse"true== 0);

      byte R = System.Convert.ToByte((double)values[0]);
      byte G = System.Convert.ToByte((double)values[1]);
      byte B = System.Convert.ToByte((double)values[2]);

      Color newColor;
      if (inverse)
        newColor = Color.FromRgb((byte)(255 - R)(byte)(255 - G)(byte)(255 - B));
      else
        newColor = Color.FromRgb(R, G, B);

      return newColor;
    }

    public object[] ConvertBack(object value, Type[] targetTypes, object parameter, System.Globalization.CultureInfo culture)
    {
      Color oldColor = (Color)value;
      double R = (double)oldColor.R;
      double G = (double)oldColor.G;
      double B = (double)oldColor.B;

      return new object[] { R, G, B };
    }

  }

  class ColorNameValueConverter : IValueConverter
  {
    private Dictionary<Color, string> namedColors = new Dictionary<Color, string>();

    public ColorNameValueConverter()
    {
      PropertyInfo[] colorProperties = typeof(Colors).GetProperties(BindingFlags.Static | BindingFlags.Public);
      Color stepColor;
      foreach (PropertyInfo pi in colorProperties)
      {
        if (pi.PropertyType == typeof(Color))
        {
          stepColor = (Color)pi.GetValue(null, null);
          namedColors[stepColor= pi.Name;
        }
      }
    }
    public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
      Color col = (Color)value;
      if(namedColors.ContainsKey(col))
        return namedColors[col];
      return DependencyProperty.UnsetValue;
    }
    public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
      throw new NotImplementedException();
    }
  }  
}
WPF I Multi Value Converter And I Value Converter
24.146.IValueConverter
24.146.1.IMultiValueConverter and IValueConverterIMultiValueConverter and IValueConverter
24.146.2.Decimal ScrollBar Window with extending IValueConverterDecimal ScrollBar Window with extending IValueConverter
24.146.3.Extend IValueConverter to create your own converterExtend IValueConverter to create your own converter
24.146.4.Debug Data Bindings Using an Empty IValueConverterDebug Data Bindings Using an Empty IValueConverter
24.146.5.Use LengthConverterUse LengthConverter
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.