2
votes

I create an object type Message than a list TableDataGrid_ItemSource = new ObservableCollection<Message>(). Before I had only DataGridTextColumn columns and they were all binding the correct types of object Message.

<DataGridTextColumn Header="Type" Binding="{Binding MessageCategoryID.Type}" Width="*"/>
<DataGridTextColumn Header="Full text" Binding="{Binding FullTextMessage}" Width="*"/> 

Now I want to customize a column by adding a text and an image both types of Message object.

<DataGridTemplateColumn Header="Message ID">
    <DataGridTemplateColumn.CellTemplate>
        <DataTemplate>
            <StackPanel Orientation="Horizontal">
                <Label Content="{Binding MessageID, Mode=OneWayToSource}" Width="*" Visibility="Visible"/>
                <Image Source="{Binding Image}" HorizontalAlignment="Left" Width="20" Height="20"></Image>
            </StackPanel>
        </DataTemplate>
    </DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>

When I run I get this error:

An unhandled exception of type 'System.Windows.Markup.XamlParseException' occurred in PresentationFramework.dll

Additional information: Provide value on 'System.Windows.Baml2006.TypeConverterMarkupExtension' threw an exception.

1
Wild guess Source="{Binding Image}" is the problem! What is the DataType of Image?Athafoud
I actually checked with a random picture in my solution and it works just fine. I think the problem is the binding from the object to datagridtemplatecolumn.Georgiana M

1 Answers

2
votes
<Label  Width="*"

this is the source of your trouble. Width="*" - it is only for Columns and Rows. Remove it. If you want to Stretch your Label and Image, replace your StackPanel by Grid. StackPanel make its children size minimal.