DataGridTemplateColumn不绑定ItemSource(WPF)中的对象

时间:2016-08-24 10:36:42

标签: c# wpf binding datagrid

我创建了一个对象类型Message而不是列表TableDataGrid_ItemSource = new ObservableCollection<Message>()。 在我只有DataGridTextColumn列之前,它们都绑定了正确类型的对象Message。

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

现在我想通过添加两种类型Message对象的文本和图像来自定义列。

<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>

当我跑步时,我收到此错误:

  

未处理的类型异常   'System.Windows.Markup.XamlParseException'发生在   PresentationFramework.dll

     

其他信息:提供价值   'System.Windows.Baml2006.TypeConverterMarkupExtension'扔了一个   异常。

1 个答案:

答案 0 :(得分:2)

<Label  Width="*"

这是你麻烦的根源。 Width="*" - 仅适用于列和行。去掉它。   如果您想要拉伸LabelImage,请将您的StackPanel替换为网格。 StackPanel让孩子的身材变得极小。