2016-08-24 67 views
2

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

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

当我跑我得到这个错误:

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.

+0

疯狂猜测'Source =“{Binding Image}”'是问题! Image的DataType是什么? – Athafoud

+1

我实际上检查了我的解决方案中的随机图片,它工作得很好。我认为问题是从对象到datagridtemplatecolumn的绑定。 –

回答

2
<Label Width="*" 

这是你的麻烦之源。 Width="*" - 仅适用于列和行。去掉它。 如果您想拉伸您的LabelImage,请用网格替换您的StackPanelStackPanel使其子女大小最小。

+0

谢谢!它现在正确绑定。 –

+0

不客气。 –