ivalueconverter in textcolumn?

3 posts, 0 answers
  1. mark
    mark avatar
    3 posts
    Member since:
    Mar 2019

    Posted 05 Mar Link to this post

    Hi there,

    I'd like to know if you can use an ivalueconverter in a textcolumn, or if not, how you can use the filter menu of a textcolumn in a templatecolumn?
    On a related note; can you also use a converter on the grouping header?

    Bit of background, for further clarity:
    I'm trying to create a program that displays a list of files, their names and things like their last edited dates.
    As such my datasource is a list(fileinfo).
    To display the filenames in a user friendly manner I'd like to trim the directorynames a bit.
    I know how to do this using a templatecolumn, but then I lose the nice filtering menu that's built into the textcolumn. 

  2. mark
    mark avatar
    3 posts
    Member since:
    Mar 2019

    Posted 05 Mar in reply to mark Link to this post

    After searching through the documentation some more I found out what I needed to convert grouping headers, you can just use the raddatagrid.groupheadertemplate property. (https://docs.telerik.com/devtools/universal-windows-platform/controls/raddatagrid/features/grouping/datagrid-styling)

    still curious about converting textboxcolumn values though

  3. Lance | Team Lead - US DevTools Support
    Admin
    Lance | Team Lead - US DevTools Support avatar
    1048 posts

    Posted 05 Mar Link to this post

    Hi Mark,

    For the best performance, I would instead recommend adding a property to the data model that simply returns the display-friendly name. 

    For example:

    public class Data
    {
        public string FilePath { get; set; }
        public string FileName => System.IO.Path.GetFileName(FilePath);
    }

    Here's the result at runtime:



    Value Converter

    If you can't do that, you can still apply a ValueConverter in a DataGridTemplateColumn because you can set a CellContentTemplate.

    <grid:RadDataGrid x:Name="grid" AutoGenerateColumns="False">
        <grid:RadDataGrid.Resources>
            <local:FilePathToFileNameConverter x:Key="FilePathToFileNameConverter"/>
        </grid:RadDataGrid.Resources>
        <grid:RadDataGrid.Columns>
            <grid:DataGridTemplateColumn x:Name="FilePathTemplateColumn">
                <grid:DataGridTemplateColumn.CellContentTemplate>
                    <DataTemplate>
                        <TextBlock Text="{Binding FilePath, Converter={StaticResource FilePathToFileNameConverter}}"/>
                    </DataTemplate>
                </grid:DataGridTemplateColumn.CellContentTemplate>
            </grid:DataGridTemplateColumn>
        </grid:RadDataGrid.Columns>
    </grid:RadDataGrid>

    The converter would look like this:

    public class FilePathToFileNameConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, string language)
        {
            try
            {
                if (value is string filePath)
                {
                    return System.IO.Path.GetFileName(filePath);
                }
            }
            catch
            {
                // ignored
            }
     
            return value;
        }
     
        public object ConvertBack(object value, Type targetType, object parameter, string language)
        {
            throw new NotImplementedException();
        }
    }

    Note: Please read the Template Column documentation to understand the implications with sorting and grouping when using this column type.

    Regards,
    Lance | Technical Support Engineer, Principal
    Progress Telerik
    Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Feedback Portal and vote to affect the priority of the items
Back to Top