This is a migrated thread and some comments may be shown as answers.

Help: About Converter and Binding

0 Answers 300 Views
GridView
This is a migrated thread and some comments may be shown as answers.
Yu
Top achievements
Rank 1
Yu asked on 07 Oct 2013, 02:25 PM

I have a enum type, and want to display it in column with image, and filter it with description. I directly convert enum type to image path, and can display it in column, but can not filter it. I convert enum type to a class with properties, such as image path, description, but can not binding it correctly.

<Window x:Class="TrafficLight.MainWindow"
        xmlns:local="clr-namespace:TrafficLight" xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" mc:Ignorable="d"
        Title="MainWindow" Height="350" Width="600">
 
    <Window.Resources>
        <local:LightConvert x:Key="Converter"/>
        <local:LightStringConvert x:Key="StringConverter"/>
        <local:LightWrpper x:Key="LightWrpper"/>
 
        <DataTemplate x:Key="ImageCellTemplate">
            <Image Height="16" Width="16" HorizontalAlignment="Center" Source="{Binding SouthNorthLight, Converter={StaticResource ResourceKey=Converter}}"/>
        </DataTemplate>
 
        <DataTemplate x:Key="LightListTemplate">
            <StackPanel Orientation="Horizontal">
                <Image HorizontalAlignment="Center" VerticalAlignment="Center" Height="16" Width="16" Source="{Binding ImagePath}"/>
                <TextBlock Text="{Binding Description}" HorizontalAlignment="Left" VerticalAlignment="Center" Padding="5,0,2,0" />
            </StackPanel>
        </DataTemplate>
         
    </Window.Resources>
 
    <Window.DataContext>
        <local:LightViewModel/>
    </Window.DataContext>
    <Grid>
 
        <telerik:RadGridView AutoGenerateColumns="False" ItemsSource="{Binding LightCollection}">
            <telerik:RadGridView.Columns>
                 
                <telerik:GridViewDataColumn Header="Location"
                                            IsReadOnly="True"
                                            DataMemberBinding="{Binding Location}"/>
 
                <telerik:GridViewImageColumn Header="South North Light"
                                             IsReadOnly="True"
                                             ImageHeight="16"
                                             ImageWidth="16"
                                             ImageStretch="Uniform"
                                             IsFilterable="False"
                                             DataMemberBinding="{Binding SouthNorthLight, Converter={StaticResource StringConverter}}"/>
 
                <telerik:GridViewComboBoxColumn Header="South North Light"
                                                IsFilterable="False"
                                                FilterMemberPath="Description"
                                                DataMemberBinding="{Binding SouthNorthLight, Converter={StaticResource Converter}}"
                                                ItemsSource="{Binding Path=LightWrpperList, Source={StaticResource LightWrpper}}"
                                                ItemTemplate="{StaticResource LightListTemplate}"/>
 
                <telerik:GridViewImageColumn Header="East West Light"
                                             IsReadOnly="True"
                                             ImageHeight="24"
                                             ImageWidth="24"
                                             ImageStretch="Uniform"
                                             FilterMemberPath="Description"
                                             SortMemberPath="Description"
                                             DataMemberBinding="{Binding EastWestLight, Converter={StaticResource Converter}}">
                    <telerik:GridViewImageColumn.CellTemplate>
                        <DataTemplate>
                            <Image HorizontalAlignment="Center" Source="{Binding Path=ImagePath}"/>
                        </DataTemplate>
                    </telerik:GridViewImageColumn.CellTemplate>
                </telerik:GridViewImageColumn>
 
                <telerik:GridViewComboBoxColumn Header="South North Light"
                                                IsFilterable="True"
                                                FilterMemberPath="Description"
                                                DataMemberBinding="{Binding SouthNorthLight, Converter={StaticResource Converter}}"
                                                CellTemplate="{StaticResource ImageCellTemplate}"
                                                ItemsSource="{Binding Path=LightWrpperList, Source={StaticResource LightWrpper}}"
                                                ItemTemplate="{StaticResource LightListTemplate}"/>
                 
            </telerik:RadGridView.Columns>
        </telerik:RadGridView>
 
    </Grid>
</Window>
 
 
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Globalization;
using System.Windows.Data;
 
namespace TrafficLight
{
    public enum Light
    {
        Green,
        Red,
        Yello,
    }
 
    public class Lights
    {
        public Light SouthNorthLight { get; set; }
 
        public Light EastWestLight { get; set; }
 
        public string Location { get; set; }
    }
 
    public class LightViewModel
    {
        public ObservableCollection<Lights> LightCollection { get; private set; }
 
        public LightViewModel()
        {
            LightCollection = new ObservableCollection<Lights>();
 
            LightCollection.Add(new Lights() { SouthNorthLight = Light.Green, EastWestLight = Light.Red, Location = "The 5th St" });
            LightCollection.Add(new Lights() { SouthNorthLight = Light.Red, EastWestLight = Light.Green, Location = "St John Rd" });
            LightCollection.Add(new Lights() { SouthNorthLight = Light.Yello, EastWestLight = Light.Red, Location = "Sents Rd" });
        }
    }
 
    public class LightWrpper
    {
 
        public static List<LightWrpper> LightWrpperList { get; private set; }
 
        public static LightWrpper Red { get; private set; }
        public static LightWrpper Green { get; private set; }
        public static LightWrpper Yello { get; private set; }
 
        static LightWrpper()
        {
            Red = new LightWrpper() { Light = Light.Red, ImagePath = "Images/Red.png", Description = "Red Light On" };
            Green = new LightWrpper() { Light = Light.Green, ImagePath = "Images/Green.png", Description = "Green Light On" };
            Yello = new LightWrpper() { Light = Light.Yello, ImagePath = "Images/Yello.png", Description = "Yello Light On" };
 
            LightWrpperList = new List<LightWrpper>();
            LightWrpperList.Add(Red);
            LightWrpperList.Add(Green);
            LightWrpperList.Add(Yello);
        }
 
        public Light Light { get; private set; }
        public string ImagePath { get; private set; }
        public string Description { get; private set; }
    }
 
    [ValueConversion(typeof(Light), typeof(LightWrpper))]
    public class LightConvert : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            if (value == null)
            {
                return LightWrpper.Yello;
            }
 
            var light = (Light)value;
            switch (light)
            {
                case Light.Green:
                    return LightWrpper.Green;
                case Light.Red:
                    return LightWrpper.Red;
                default:
                    return LightWrpper.Yello;
            }
        }
 
        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            if (value == null)
            {
                return Light.Yello;
            }
 
            var light = (LightWrpper)value;
            switch (light.Light)
            {
                case Light.Green:
                    return Light.Green;
                case Light.Red:
                    return Light.Red;
                default:
                    return Light.Yello;
            }
        }
    }
 
    [ValueConversion(typeof(Light), typeof(string))]
    public class LightStringConvert : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            var light = (Light)value;
            switch (light)
            {
                case Light.Green:
                    return "Images/Green.png";
                case Light.Red:
                    return "Images/Red.png";
                default:
                    return "Images/Yello.png";
            }
        }
 
        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            throw new NotImplementedException();
        }
    }
 
}






No answers yet. Maybe you can help?

Tags
GridView
Asked by
Yu
Top achievements
Rank 1
Share this question
or