Bindable CellContentFormat into DataGridTextColumn

2 posts, 0 answers
  1. Johan Stiver
    Johan Stiver avatar
    1 posts
    Member since:
    May 2015

    Posted 12 May 2015 Link to this post

    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.

     

  2. Ves
    Admin
    Ves avatar
    2879 posts

    Posted 15 May 2015 Link to this post

    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
Back to Top