b

Wednesday, 26 December 2012

ivalueconverter color converter in wpf

ivalueconverter color converter in wpf

Step1) Create a WPF Project
Step 2) Add a datagrid to XAML

Step 3) Add a class
      public class ColorDemo
    {
        public int ID { get; set; }
        public String color { get; set; }
    }

Step 4) Build data

 void BuildData()
        {
            listColor.Add(new ColorDemo { ID=1, color="red"});
            listColor.Add(new ColorDemo { ID = 2, color = "green" });
            listColor.Add(new ColorDemo { ID = 3, color = "blue" });

        }

Step 5) Add a class for Color Conversion

namespace SilverlightApplication1
{
    public class colorconv:IValueConverter
    {

        public object Convert(object value, Type targetType, object parameter,                            System.Globalization.CultureInfo culture)
        {
            String cc = value as String;
            String xamlString = "<Canvas xmlns=\"http://schemas.microsoft.com/winfx/2006/xaml/presentation\" Background=\""+cc+"\"/>";
            Canvas c = (Canvas)System.Windows.Markup.XamlReader.Load(xamlString);
            SolidColorBrush mistyRoseBrush = (SolidColorBrush)c.Background;
            Color mistyRose = mistyRoseBrush.Color;
            SolidColorBrush b = new SolidColorBrush(mistyRose);
            return b;

        }

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

Step 6)  Bind List data to dataGrid

void Bind()
        {
            dataGrid1.ItemsSource = listColor;
        }

Step 7) Run the Application
               Output:


Complete Source Code

Page1.xaml.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Windows.Navigation;

namespace SilverlightApplication1
{
    public partial class Page1 : Page
    {
        List<ColorDemo> listColor = new List<ColorDemo>();
        public Page1()
        {
            InitializeComponent();
            BuildData();
            Bind();
        }

        // Executes when the user navigates to this page.
        protected override void OnNavigatedTo(NavigationEventArgs e)
        {
        }

        void Bind()
        {
            dataGrid1.ItemsSource = listColor;
        }
        void BuildData()
        {
            listColor.Add(new ColorDemo { ID=1, color="red"});
            listColor.Add(new ColorDemo { ID = 2, color = "green" });
            listColor.Add(new ColorDemo { ID = 3, color = "blue" });

            Color c =  Color.FromArgb(255,255,255,0);
            //rectangle1.Fill = new SolidColorBrush(c);
        }
    }


    public class ColorDemo
    {
        public int ID { get; set; }
        public String color { get; set; }
    }
}

Page.xaml

<navigation:Page x:Class="SilverlightApplication1.Page1"
           xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
           xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
           xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
           xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
           mc:Ignorable="d"
               
           xmlns:navigation="clr-namespace:System.Windows.Controls;assembly=System.Windows.Controls.Navigation"
           d:DesignWidth="640" d:DesignHeight="480"
           Title="Page1 Page" xmlns:sdk="http://schemas.microsoft.com/winfx/2006/xaml/presentation/sdk"
             xmlns:sp="clr-namespace:SilverlightApplication1"
                 >
    <Grid x:Name="LayoutRoot">
        <Grid.Resources>
            <sp:colorconv x:Key="colorLocal"></sp:colorconv>
        </Grid.Resources>
            <sdk:DataGrid AutoGenerateColumns="False" Height="144" HorizontalAlignment="Left" Margin="21,67,0,0" Name="dataGrid1" VerticalAlignment="Top" Width="559">
            <sdk:DataGrid.Columns>
                <sdk:DataGridTextColumn  Header="ID" Binding="{Binding ID}"></sdk:DataGridTextColumn>
                <!--<sdk:DataGridTextColumn  Header="Color" Binding="{Binding Path=color, Converter={StaticResource colorLocal}}" ></sdk:DataGridTextColumn>-->
                <sdk:DataGridTemplateColumn Header="Color">
                    <sdk:DataGridTemplateColumn.CellTemplate>
                        <DataTemplate>
<Rectangle Height="10" HorizontalAlignment="Left" Margin="10,10,0,0" Name="rectangle1"
           Stroke="Black" StrokeThickness="1"
Fill="{Binding Path=color, Converter={StaticResource colorLocal}}"
          
           VerticalAlignment="Top" Width="20" />
                            <!--<sdk:Label Height="28" HorizontalAlignment="Left"
                                       Margin="10,10,0,0" Name="label1"
                                       VerticalAlignment="Top" Width="120"
                                       Content="colored text"
                                       Foreground="{Binding Path=color, Converter={StaticResource colorLocal}}" />-->
                        </DataTemplate>
                    </sdk:DataGridTemplateColumn.CellTemplate>
                </sdk:DataGridTemplateColumn>
            </sdk:DataGrid.Columns>
        </sdk:DataGrid>
        <TextBlock Height="23" HorizontalAlignment="Left" Margin="10,10,0,0" Name="textBlock1" Text="IValueConvertor for String to Color conversion and Binding in dataGrid" VerticalAlignment="top" FontSize="16"/>
    </Grid>
</navigation:Page>

colorconv.cs


using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Windows.Data;
namespace SilverlightApplication1
{
    public class colorconv:IValueConverter
    {

        public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            String cc = value as String;
            String xamlString = "<Canvas xmlns=\"http://schemas.microsoft.com/winfx/2006/xaml/presentation\" Background=\""+cc+"\"/>";
            Canvas c = (Canvas)System.Windows.Markup.XamlReader.Load(xamlString);
            SolidColorBrush mistyRoseBrush = (SolidColorBrush)c.Background;
            Color mistyRose = mistyRoseBrush.Color;
            SolidColorBrush b = new SolidColorBrush(mistyRose);
            return b;//mistyRose;
          //Colors.
        }

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


No comments:

Post a Comment