Home Forums WPF controls Xceed DataGrid for WPF Generic IValueConverter

Viewing 11 posts - 1 through 11 (of 11 total)
  • Author
    Posts
  • User (Old forums)
    Member
    Post count: 23064
    #23223 |

    I just thought I would share with the community.
    This might be helpful for all of you that implement a lot of converters in your DataBinding.

    I wrote a Base object that implements IValueConverter But the object is Generic so it helps speed up coding by not having to always rewrite code to check to make sure the types are correct before you convert them.

    Here is the code:

    <code>
    public abstract class BValueConverter<T1,T2>:IValueConverter
    {

    #region IValueConverter Members

    public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
    if (value.GetType() == typeof(T1))
    return Convert((T1)value, targetType, parameter, culture);
    if (value.GetType() == typeof(T2))
    return value;
    return null;
    }

    public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
    if (value.GetType() == typeof(T2))
    return ConvertBack((T2)value,targetType,parameter,culture);
    if (value.GetType() == typeof(T1))
    return value;
    return null;
    }

    #endregion

    #region IValueConverterT<T1,T2> Members

    public abstract T2 Convert(T1 value, Type targetType, object parameter, System.Globalization.CultureInfo culture);

    public abstract T1 ConvertBack(T2 value, Type targetType, object parameter, System.Globalization.CultureInfo culture);

    #endregion
    };

    </code>

    Imported from legacy forums. Posted by MiddleTommy (had 4686 views)

    User (Old forums)
    Member
    Post count: 23064

    And an example of its use:
    <code>
    [ValueConversion(typeof(DateTime), typeof(string))]
    public class DateConverter : BValueConverter<DateTime,string>
    {
    private readonly DateTime nullDate = new DateTime(1948,1,1);

    public override string Convert(DateTime value, Type targetType, object parameter, CultureInfo culture)
    {
    DateTime date = value;
    if (date == nullDate) return “”;
    if (parameter == null)
    return date.ToString(“MM/dd hh:mm tt”);
    return date.ToString((string)parameter);
    }

    public override DateTime ConvertBack(string value, Type targetType, object parameter, CultureInfo culture)
    {
    DateTime resultDateTime;
    if (DateTime.TryParse(value, out resultDateTime))
    {
    return resultDateTime;
    }
    return nullDate;
    }
    }

    </code>

    I use 1/1/1948 as a null Date because that is the year my company started to no Dates will be less that it.

    Imported from legacy forums. Posted by MiddleTommy (had 944 views)

    User (Old forums)
    Member
    Post count: 23064

    Could you provide some example XAML to show how this could be used to format data bound to a BindingList? In particular, I am not sure where/how the converter tag should be specified. Much thanks!

    I was hoping there would be some examples in the Xceed documentation showing how do accomplish this; I have not found any.

    Imported from legacy forums. Posted by Ed (had 821 views)

    Xceed Support
    Member
    Post count: 5658

    There is an example of a converter in the <a href=”http://doc.xcd/XceedWpfDataGrid/Styling%20Elements.html”>Styling Elements</a> How-to section and I have plans to add more in an upcoming release.

    Another location you could try when you can’t find the information you are looking for is in the theme folder (under the Xceed DataGrid for WPF installation folder). The XAML files contain may different scenarios that can point you in the right direction.

    If you have any other documentation feedback, please let me know 🙂

    Imported from legacy forums. Posted by Jenny [Xceed] (had 562 views)

    User (Old forums)
    Member
    Post count: 23064

    I use the converters in custom cell DataTemplates
    for example to convert a field VenderID to the VenderName

    Ill post some examples here.

    Imported from legacy forums. Posted by MiddleTommy (had 513 views)

    User (Old forums)
    Member
    Post count: 23064

    —————————from Templates.xaml

    <ResourceDictionary x:Class=”MyCards.Data.Tools.Templates”
    xmlns=”http://schemas.microsoft.com/winfx/2006/xaml/presentation&#8221;
    xmlns:x=”http://schemas.microsoft.com/winfx/2006/xaml&#8221;
    xmlns:mp=”clr-namespace:MyCards.Data.Tools”
    xmlns:xcd=”clr-namespace:Xceed.Wpf.Controls;assembly=Xceed.Wpf.Controls”
    xmlns:xcdg=”http://schemas.xceed.com/wpf/xaml/datagrid”&gt;

    <mp:DateConverter x:Key=”DateConverter”/>

    <DataTemplate x:Key=”DateCellDataTemplate”>
    <TextBlock Text=”{Binding Converter={StaticResource DateConverter}}”/>
    </DataTemplate>
    <DataTemplate x:Key=”ShortDateCellDataTemplate”>
    <TextBlock Text=”{Binding Converter={StaticResource DateConverter}, ConverterParameter=MM/dd/yy}”/>
    </DataTemplate>

    </ResourceDictionary>

    —————————————————–in Templates.cs code file

    public partial class Templates:ResourceDictionary
    {

    public DataTemplate getCellTemplate(string templateKey)
    {
    DataTemplate dt = this[templateKey] as DataTemplate;
    return dt;
    }
    }

    ———————————————–some code file where grid is.

    //Xceed.Wpf.DataGrid.Column column;
    column.CellContentTemplate = temps.getCellTemplate(“ShortDateCellDataTemplate”);

    Imported from legacy forums. Posted by MiddleTommy (had 5756 views)

    User (Old forums)
    Member
    Post count: 23064

    I’m trying to implement what you just wrote above,should this ResourceDictionary be a resource or a embedded resource,seemed to get null in the Templates getCellTemplate()

    Imported from legacy forums. Posted by Dune (had 927 views)

    User (Old forums)
    Member
    Post count: 23064

    I have the resource dictionary in a separate xaml file with codebehind.

    then I include it into windows and such like so

        <Window.Resources>
            <ResourceDictionary>
                <ResourceDictionary.MergedDictionaries>
                    <ResourceDictionary Source=”Styles.xaml”/>
                    <Windows:Templates/>
                </ResourceDictionary.MergedDictionaries>
            </ResourceDictionary>
        </Window.Resources>

    the  <ResourceDictionary Source=”Styles.xaml”/> is a xaml file in the current project

    the  <Windows:Templates/> is a Resource Dictionary in a separate assembly with

    xmlns:Windows=”clr-namespace:MyCards.Windows;assembly=MyCards.Windows” //Or what ever assembly you have

    Templates is defined as

    <ResourceDictionary 
        xmlns=”http://schemas.microsoft.com/winfx/2006/xaml/presentation&#8221;
        xmlns:x=”http://schemas.microsoft.com/winfx/2006/xaml&#8221;
        xmlns:xcd=”clr-namespace:Xceed.Wpf.Controls;assembly=Xceed.Wpf.Controls”
        xmlns:xcdg=”http://schemas.xceed.com/wpf/xaml/datagrid&#8221;
        xmlns:sys=”clr-namespace:System;assembly=mscorlib”
        xmlns:edm=”clr-namespace:MyCards.LightSpeed;assembly=MyCards.LightSpeed”
        xmlns:Dynamic=”clr-namespace:MyCards.Dynamic;assembly=MyCards.Dynamic”
        x:Class=”MyCards.Windows.Templates” >

     let me know if this is a push in the right direction or if I just completly confussed you

    Design time might give you some Does not exist warnings (Cider bugs) but runtime works fine

    also this is defined in the templates resouce dictionary

        <Dynamic:DateConverter x:Key=”DateConverter”/>//This is a standard wpf converter defined in another assemlby
     

    Imported from legacy forums. Posted by MiddleTommy (had 535 views)

    User (Old forums)
    Member
    Post count: 23064

    Thanks for your reply and code.I managed to get it working in a different way for now.The thing is that I have a project (vs08) that has the grid related code which gets metadata,constructs columns and fills data etc.I wanted to include your further above mentioned code in that project(so that I could maintain the code there),but the issue was that I was unable to get DataTemplate dt = this[templateKey] as DataTemplate; value for that dt was null.I can’t really say if partial class Templates and its xaml file where working because I had created a resource dictonary no cs file created with it.I moved BValueConverter.cs and DateConverter.cs to my style project which has the main dictonary which gets loaded,plus the code was uable to find the resource and added

     

     <‘Styles’ :DateConverter x:Key=”DateConverter”/>

      <DataTemplate x:Key=”DateCellDataTemplate”>
        <TextBlock Text=”{Binding Converter={StaticResource DateConverter}}”/>
      </DataTemplate>
      <DataTemplate x:Key=”ShortDateCellDataTemplate”>
        <TextBlock Text=”{Binding Converter={StaticResource DateConverter}}”/>
      </DataTemplate>

     in the resource dictonary xaml file there which is loaded or intialized at runtime.Then used

    Application.Current.FindResource(templateKey) as DataTemplate; (templateKey =>column.CellContentTemplate = Tmp.getCellTemplate(“ShortDateCellDataTemplate”);)

    Well it works,but a way to generalize your code in a self contained project would have been nice,have to look at it since the way I load resources is different…………………………………………………….

    ………….

    ..

    .

     

    Imported from legacy forums. Posted by Dune (had 1170 views)

    User (Old forums)
    Member
    Post count: 23064

    Perhaps your xaml file and the code behind file were not in the same namespace? (Meaning they were two different objects.)

    Or the templateKey string supplied was spelled wrong.

    Those are some problems I had setting this up

    You can check by redefining

    public partial class Templates:ResourceDictionary

    as

    public partial class Templates//remove redundant ResourceDictionary declaration

    if at runtime Templates is still a ResourceDictionary then you are certain something else is wrong

     

    Imported from legacy forums. Posted by MiddleTommy (had 1282 views)

    User (Old forums)
    Member
    Post count: 23064

    Thanks for your reply and follow up.I managed to get it working in the way you mentioned above after creating a proper Templates.xaml and Templates.cs file which were related and adding that resource in the loader code and well now the resource is found and also the code is in the sample place as grid related code,this is good for managing.Well only thing was I added a check if value is null in your BValueConverter well cause some times my date was never entered in the first place.

    Imported from legacy forums. Posted by Dune (had 1383 views)

Viewing 11 posts - 1 through 11 (of 11 total)
  • You must be logged in to reply to this topic.