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

ivalueconverter in textcolumn?

2 Answers 71 Views
DataGrid
This is a migrated thread and some comments may be shown as answers.
mark
Top achievements
Rank 1
mark asked on 05 Mar 2019, 09:39 AM

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 Answers, 1 is accepted

Sort by
0
mark
Top achievements
Rank 1
answered on 05 Mar 2019, 09:55 AM

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

0
Lance | Manager Technical Support
Telerik team
answered on 05 Mar 2019, 03:51 PM
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
Tags
DataGrid
Asked by
mark
Top achievements
Rank 1
Answers by
mark
Top achievements
Rank 1
Lance | Manager Technical Support
Telerik team
Share this question
or