I'm trying to build a generic converter to Excel for my DataGrid. I'm using WPF 4.0 and a DataGrid User Control. This converter should take all values from this grid (or selected rows of it) and export to excel (by saving a file). It works, but i'm having some trouble with DataGridTemplateColumns, because DataGridTemplateColumns doesn't accept directly binding in WPF (i use DataGridTemplateColumns to show an text and a image inside a cell, like this => [ ] "Yes" - Yes is an Text and [ ] an image, so it's one binding to TextBlock and another to Image). This is an example of a DataGrid:
<ct:DataGridExtended AutoGenerateColumns="False"
HorizontalAlignment="Stretch"
VerticalAlignment="Stretch"
IsReadOnly="True"
FrozenColumnCount="7"
Margin="-1,59,-1,25"
Grid.Column="2"
ScrollViewer.IsDeferredScrollingEnabled="True"
ct:DataGridDoubleClick.ExecuteCommand="{Binding InformacoesPeca}"
ItemsSource="{Binding Placas, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"
SelectedItem="{Binding PlacasSelectedItem, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"
SelectedIndex="{Binding PlacasSelectedIndex, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"
ut:SelectedItemsBehavior.SelectedItemsChangedHandler="{Binding Path=ResultsSelectionChangedCommand}">
<DataGrid.Columns>
<DataGridTextColumn Header="Cone Seq. Orig."
Binding="{Binding OriginConeSeq}"
Visibility="Hidden" />
<DataGridTextColumn Header="Cone"
Binding="{Binding Cone.ConeId}" />
<DataGridTemplateColumn Header="Nec. Acomp.">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<Grid>
<Image Source="{Binding MonitoringNeed,Converter={StaticResource YesNoInstructionImageConverter}}"
Height="16"
Width="16"
HorizontalAlignment="Left"
Margin="2,0,0,0" />
<TextBlock Text="{Binding MonitoringNeed,Converter={StaticResource NoEmptyStringConverter}}"
HorizontalAlignment="Left"
Margin="22,0,0,0" />
</Grid>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
</DataGrid.Columns>
</DataGridExtended>
So, i have no problem to get data from a object using Binding from a DataGridTextColumn, but i couldn't figure out how to take Binding from DataGridTemplateColumn.
I've studied a lot of examples, including some here (like this: Get textbox binding in wpf datagridtemplatecolumn) on StackOverflow and still i'm stuck.
There's another problem but this one i've resolved: I need to take only those columns who's visible to user. I resolved using the Visibility Property of DataGridColumn, then i'm trying to resolve that TemplateColumn problem by getting only the value from that cell.
I hope I have been able to explain my problem.
Thanks for any help. Gustavo.