2016-08-24 69 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

不客氣。 –