views:

160

answers:

1

I'm trying to display rich text inside of a column of a WPF DataGrid (from WPF Toolkit). Something like this:

Name: Bob
Title: Doctor

I am creating a data object programmatically in code with the string property. And I want this string to contain the rich text and than bind it to the column contents. Is that possible?

Would really appreciate any help!

+1  A: 

Use the DataGridTemplateColumn:

<dg:DataGridTemplateColumn Header="Info">
    <dg:DataGridTemplateColumn.CellTemplate>
        <DataTemplate>
            <Grid>
                <Grid.RowDefinitions>
                    <RowDefinition />
                    <RowDefinition />
                </Grid.RowDefinitions>
                <Grid.ColumnDefinitions>
                    <ColumnDefinition />
                    <ColumnDefinition />
                </Grid.ColumnDefinitions>
                <TextBlock Text="Name:" FontWeight="Bold" Grid.Row="0" Grid.Column="0" />
                <TextBlock Text="{Binding Name}" Grid.Row="0" Grid.Column="1" />

                <TextBlock Text="Title:" FontWeight="Bold" Grid.Row="1" Grid.Column="0" />
                <TextBlock Text="{Binding Title}" Grid.Row="1" Grid.Column="1" />
            </Grid>
        </DataTemplate>
    </dg:DataGridTemplateColumn.CellTemplate>
</dg:DataGridTemplateColumn>
RandomEngy
Thanks, but as I said above, I am creating a data object programmatically in code. I want to build a string like this: "<b>Name:</b> Bob\n<b>Title:</b> Doctor" and assign it to the column. Is that possible?
Greg R
That can work if you're creating the data object programmatically. Just make sure the type in your collection has Name and Title properties. I updated the example to show how it can bind to them.
RandomEngy
Thanks, I figured you can get it done this way, but I was hoping if Name and Title were not 2 set properties, but there might be any number of them, it would be great to build up the layout programmatically and not depend on preset template. Is that possible?
Greg R
Perhaps. You could always set the DataTemplate programatically, adding elements and bindings in code. There might be a way to do it with a binding Converter that parses the string and makes it into a FlowDocument. But I would only go that route if I was sure that normal databinding wasn't going to work.
RandomEngy
Since I can't find a better way of doing this, I am marking this as good enough answer :)
Greg R