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

Bindable CellContentFormat into DataGridTextColumn

1 Answer 109 Views
DataGrid
This is a migrated thread and some comments may be shown as answers.
This question is locked. New answers and comments are not allowed.
Johan Stiver
Top achievements
Rank 1
Johan Stiver asked on 12 May 2015, 10:02 PM

Hi Everyone...

is possible to bind a custom StringFormat into CellContentFormat property for DataGridTextColumn??

 i mean, probably i have different columns values, someones must be showed as currency values ({0:C}) and the other ones must be showed as a percentage value ({0:P}), i try to bind directly to a property called "CustomFormat" in the xaml like this:

 <dataGrid:DataGridTextColumn Header="Header" PropertyName="Value" CellContentFormat="{Binding CustomFormat}" >

    <dataGrid:DataGridTextColumn.HeaderStyle>
        <Style TargetType="primitives:DataGridColumnHeader">
            <Setter Property="HorizontalContentAlignment" Value="Right"/>
            <Setter Property="FontSize" Value="22" />
        </Style>
    </dataGrid:DataGridTextColumn.HeaderStyle>
</dataGrid:DataGridTextColumn>

 Can anyone suggest me something??

 i´ll appreciate your help.

 

1 Answer, 1 is accepted

Sort by
0
Ves
Telerik team
answered on 15 May 2015, 01:13 PM
Hi Johan,

Let me start with a question, so that I am sure I am getting this right -- you might have different kind of values within the same column, so that you might need one format on item 1 (row 1) and another on item 2, is that correct? In this case, the short answer is -- this cannot be done through CellContentFormat property of the column.

You can use DataGridTemplateColumn instead. You can place just a single TextBlock within the CellContentTemplate and bind it to the DataContext. Then you can add a converter, which in this case, will receive the entire business object, so that you can retrieve the value, the format and return the text to be shown as per your requirements.

<telerikGrid:DataGridTemplateColumn>
    <telerikGrid:DataGridTemplateColumn.CellContentTemplate>
        <DataTemplate>
            <TextBlock Text="{Binding Converter={StaticResource converter}}"></TextBlock>
        </DataTemplate>
    </telerikGrid:DataGridTemplateColumn.CellContentTemplate>
</telerikGrid:DataGrid

And the converter might be similar to this:

public object Convert(object value, System.Type targetType, object parameter, string language)
        {
            MyDataItem data = value as MyDataItem;
            return data.Value.ToString(data.Format);
        }


Best regards,
Ves
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
Johan Stiver
Top achievements
Rank 1
Answers by
Ves
Telerik team
Share this question
or