我创建的对象类型是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.
疯狂猜测'Source =“{Binding Image}”'是问题! Image的DataType是什么? – Athafoud
我实际上检查了我的解决方案中的随机图片,它工作得很好。我认为问题是从对象到datagridtemplatecolumn的绑定。 –