Your Privacy Matters: We use our own and third-party cookies to improve your experience on our website. By continuing to use the website we understand that you accept their use. Cookie Policy
590
XamDataGrid Field Converter with ConverterParameter
posted

I'm having a problem using a converter on Fields in the XamDataGrid that works when I use an UnboundField. I have an integer value that I want to display leading zeros if the value is less than 10. i.e. value 1 should show up as 01

I'm including the source for a small example that shows the UnboundField working and the Field not working.

Here's my xaml:

<Window x:Class="WpfApplication1.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:local="clr-namespace:WpfApplication1"
        xmlns:igDP="http://infragistics.com/DataPresenter"
        DataContext="{Binding RelativeSource={RelativeSource Self}}" >
    <Grid>
        <Grid.Resources>
            <local:StringFormatConverter x:Key="StringFormatConverter"/>
        </Grid.Resources>
        <igDP:XamDataGrid DataSource="{Binding Path=MyRecords}" >
            <igDP:XamDataGrid.FieldLayouts>
                <igDP:FieldLayout>
                    <igDP:FieldLayout.Fields>                        
                        <igDP:Field Name="ID" Converter="{StaticResource StringFormatConverter}" ConverterParameter="{}{0:00}" />
                        <igDP:UnboundField Label="ID-Unbound" BindingPath="ID" Converter="{StaticResource StringFormatConverter}" ConverterParameter="{}{0:00}" />
                        <igDP:Field Name="Name" />
                    </igDP:FieldLayout.Fields>
                </igDP:FieldLayout>
            </igDP:XamDataGrid.FieldLayouts>
        </igDP:XamDataGrid>
    </Grid>
</Window>

And here's my code behind.


using System;
using System.Collections.Generic;
using System.Globalization;
using System.Windows;
using System.Windows.Data;
 
namespace WpfApplication1
{
    public partial class MainWindow : Window
    {
        public class MyRecord
        {
            public int ID { getset; }
            public string Name { getset; }
 
            public MyRecord(int id, string name)
            {
                ID = id;
                Name = name;
            }
        }
 
        public List<MyRecord> MyRecords { getset; }
        public MainWindow()
        {
            MyRecords = new List<MyRecord>();
            MyRecords.Add(new MyRecord(0, "Bob"));
            MyRecords.Add(new MyRecord(0, "Frank"));
            MyRecords.Add(new MyRecord(1, "John"));
            MyRecords.Add(new MyRecord(10, "Mary"));
 
            InitializeComponent();
        }
 
    }
 
    public class StringFormatConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            return string.Format(parameter.ToString(), value);
        }
 
        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            return value;
        }
    }
}

Any ideas?

  • 135
    posted

    Hello,

    Thank you for verifying the thread as answered, it will help other users may take better advantage of it.

    I am just checking if the latest reply helped you out and if you require any further assistance on the matter.

  • 135
    Verified Answer
    posted

    Hello,

    Thank you for your patience and cooperation on this case.

    I have further research on this case and found that since the converter is changing the value to a string type, you should set the EditAsType to string in the Field setting in order for the value to display with the leading zero:

    <igDP:Field Name="ID" Converter="{StaticResource StringFormatConverter}" ConverterParameter="{}{0:00}" >
                                <igDP:Field.Settings>
                                    <igDP:FieldSettings EditAsType="sys:String"/>
                                </igDP:Field.Settings>
                            </igDP:Field>

    Also, please note if the field is editable, the ConvertBack function of the Converter should also be modified to return the correct type for the underlying property.

    I have attached an isolated sample application for your reference. Please test this and let me know incase if you have any issue as it is working fine. Thank you.
        

    BaseDataGrid.zip
  • 135
    posted

    Hello,

    Thank you for contacting Infragistics!

    We received your support request concerning “XamDataGrid Field Converter with ConverterParameter.”and this case has been assigned to me. Infragistics is dedicated to helping you solve this issue. I am working on this case and I will update the case after my finding on this. Thank you.

    Sincerely,
    Chandradeep,
    Developer Support Engineer, MCTS 
    Infragistics
    www.infragistics.com/support