0

As we can add expression in Data Column of Data Table, is it possible to create Column like DataGridTextColumn and set Binding Expression? If Yes then How?

As I am creating My Own DataGridComputedColumn For WPF DataGrid, which allows to set expression for that column so that at run time when DataSource is attached it automatically evaluates this Computed Column at run time.

For Example I have table EMP With 2 Columns FirstName and LastName. This will be DataGridTextColumn in WPF DataGrid. Now the third column will be My own Custom DataGridComputedColumn Which has property like expression in which i can set expression as FirstName +' ' + LastName which on run time evaluates and generate third column.

Thanks Visshal

1 Answer 1

2

You can't have an expression in your binding property. However you can write it with an IValueConverter:

public class FirstAndLastnameConverter : IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
    {
        var emp = value as Employee;
        return  string.Format("{0} {1}", emp.FirstName, emp.LastName);
    }

    public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
    {
        return null;
    }
}

And in your XAML:

<Window.Resources>
    <me:FirstAndLastnameConverter x:Key="converter" />
</Window.Resources>
...
<DataGrid>
    <DataGrid.Columns>
        <DataGridTextColumn Binding="{Binding Path=FirstName}"/>
        <DataGridTextColumn Binding="{Binding Path=LastName}"/>
        <DataGridTextColumn Binding="{Binding Converter={StaticResource converter}}"/>
        ...
    </DataGrid.Columns>
</DataGrid>
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.