Hello all,
I have defined a DataTemplate for one of my classes (MultiRunSimObj). This object also contains an ObservableCollection (SimObjects). I bind a DataGrid in my DataTemplate to this collection.
I want to be able to export this datagrid to excel file (preferable with all the formatting). I do not want to export the hidden columns though.
I can put a button for export on the DataTemplate, but don't know where to go after that. Since this is a DataTemplate, I do not have the luxury of code behind (anyways I want to avoid that for MVVM). Please guide.
Here is my DataTemplate (not showing the code for all the columns as there are > 100 in number).
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:my="clr-namespace:Simulators;assembly=Simulators"
xmlns:mv="clr-namespace:WPFResources;assembly=WPFResources"
>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="..\..\BaseStyles.xaml"/>
<ResourceDictionary Source="..\..\Styles\Brushes.xaml"/>
<ResourceDictionary Source="..\..\Styles\Buttons.xaml"/>
<ResourceDictionary>
<DataTemplate DataType= "{x:Type my:MultiRunSimObj}" >
<Grid IsEnabled="{Binding SimPending, Converter={StaticResource NotConvert}}">
<DockPanel LastChildFill="True" >
<Expander Header="Description" FontSize="12" DockPanel.Dock="Bottom" IsExpanded="false">
<TextBox Name="txtDescription" Height="50" VerticalScrollBarVisibility="Visible" AcceptsReturn="True" Foreground="blue"
Text="{Binding Path=Description}"/>
</Expander>
<StackPanel>
<Grid>
<DataGrid x:Name="GV_Run" ClipboardCopyMode="IncludeHeader" FrozenColumnCount="1"
ItemsSource="{Binding Path=SimObjects}" AutoGenerateColumns="False" RowDetailsVisibilityMode="Collapsed"
CanUserReorderColumns="False" CanUserSortColumns="False"
GridLinesVisibility="All" VerticalGridLinesBrush="#FF89C696" HorizontalGridLinesBrush="#FF89C696"
FontSize="12" FontStyle="Normal" FontFamily="Courier New" HorizontalContentAlignment="Center" VerticalContentAlignment="Center"
>
<DataGrid.Resources>
<mv:BindingProxy x:Key="proxy" Data="{Binding SimObj}" />
<Style TargetType="{x:Type DataGridRowHeader}">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="DataGridRowHeader">
<Border Padding="2">
<StackPanel Orientation="Horizontal">
<Button Name="btnLaunchObj" ToolTip="Settings" Height="16" Width="16"
Style="{StaticResource SettingsButtonStyle}"
Command="{Binding LaunchControlModule}" Margin="0,1,2,1">
</Button>
<Popup x:Name="popupCurrentObj"
StaysOpen="False"
PlacementTarget="{Binding ElementName=UCMain}"
Placement="Center"
PopupAnimation="Fade"
IsOpen="{Binding ShowControlModule}"
>
<Border BorderBrush="Maroon" BorderThickness="2" removed="#FFEA9D" Padding="5">
<ContentControl Content="{Binding}"/>
</Border>
</Popup>
</StackPanel>
</Border>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
<Style TargetType="DataGridRow">
<Style.Triggers>
<DataTrigger Binding="{Binding strRunState}" Value="0">
<Setter Property="Background" Value="White" />
</DataTrigger>
<DataTrigger Binding="{Binding strRunState}" Value="1">
<Setter Property="Background" Value="LightGreen" />
</DataTrigger>
<DataTrigger Binding="{Binding strRunState}" Value="2">
<Setter Property="Background" Value="Red" />
</DataTrigger>
</Style.Triggers>
</Style>
</DataGrid.Resources>
<DataGrid.RowDetailsTemplate>
<DataTemplate>
<ContentControl Content="{Binding}"/>
</DataTemplate>
</DataGrid.RowDetailsTemplate>
<DataGrid.Columns>
<DataGridTextColumn Header="Run ID" Binding="{Binding Path=RunID, Mode=TwoWay}"/>
<DataGridTextColumn Visibility="{Binding Data.ControlModule.BoundaryConditions.IU_BCs.selDBi,
Converter={StaticResource BoolToVisConverter}, Source={StaticResource proxy}, FallbackValue=Collapsed}" Header="DBi" Binding="{Binding Path=ControlModule.BoundaryConditions.IU_BCs.DBi, Mode=TwoWay}"/>
<DataGridTextColumn Header="{Binding Data.ControlModule.BoundaryConditions.IU_BCs.GetMoistAirPropDisplay, Source={StaticResource proxy}}" Visibility="{Binding Data.ControlModule.BoundaryConditions.IU_BCs.selAMoistPropValue,
Converter={StaticResource BoolToVisConverter}, Source={StaticResource proxy}, FallbackValue=Collapsed}" Binding="{Binding Path=ControlModule.BoundaryConditions.IU_BCs.AMoistPropValue, Mode=TwoWay}"/>
<DataGridTextColumn Header="{Binding Data.ControlModule.BoundaryConditions.IU_BCs.GetAirflowDisplay, Source={StaticResource proxy}}" Visibility="{Binding Data.ControlModule.BoundaryConditions.IU_BCs.selAirflow,
Converter={StaticResource BoolToVisConverter}, Source={StaticResource proxy}, FallbackValue=Collapsed}" Binding="{Binding Path=ControlModule.BoundaryConditions.IU_BCs.AirflowValue, Mode=TwoWay}"/>
<DataGridTextColumn Header="{Binding Data.ControlModule.BoundaryConditions.IU_BCs.GetPatm_AltDisplay, Source={StaticResource proxy}}" Visibility="{Binding Data.ControlModule.BoundaryConditions.IU_BCs.selPatm_AltValue,
Converter={StaticResource BoolToVisConverter}, Source={StaticResource proxy}, FallbackValue=Collapsed}" Binding="{Binding Path=ControlModule.BoundaryConditions.IU_BCs.Patm_AltValue, Mode=TwoWay}"/>
<DataGridTextColumn Visibility="{Binding Data.ControlModule.BoundaryConditions.IU_BCs.selESP,
Converter={StaticResource BoolToVisConverter}, Source={StaticResource proxy}, FallbackValue=Collapsed}" Header="ESP" Binding="{Binding Path=ControlModule.BoundaryConditions.IU_BCs.ESP, Mode=TwoWay}"/>
<DataGridTextColumn Visibility="{Binding Data.ControlModule.AirCoil1BC.selDBi,
Converter={StaticResource BoolToVisConverter}, Source={StaticResource proxy}, FallbackValue=Collapsed}" Header="Coil1 DBi" Binding="{Binding Path=ControlModule.AirCoil1BC.DBi, Mode=TwoWay}"/>
<DataGridTextColumn Header="{Binding Data.ControlModule.AirCoil1BC.GetMoistAirPropDisplay, Converter={StaticResource DisplayConverter}, ConverterParameter=Coil1 ;, Source={StaticResource proxy}}" Visibility="{Binding Data.ControlModule.AirCoil1BC.selAMoistPropValue,
Converter={StaticResource BoolToVisConverter}, Source={StaticResource proxy}, FallbackValue=Collapsed}" Binding="{Binding Path=ControlModule.AirCoil1BC.AMoistPropValue, Mode=TwoWay}"/>
<DataGridTextColumn Header="{Binding Data.ControlModule.AirCoil1BC.GetAirflowDisplay, Source={StaticResource proxy}}" Visibility="{Binding Data.ControlModule.AirCoil1BC.selAirflow,
Converter={StaticResource BoolToVisConverter}, Source={StaticResource proxy}, FallbackValue=Collapsed}" Binding="{Binding Path=ControlModule.AirCoil1BC.AirflowValue, Mode=TwoWay}"/>
<DataGridTextColumn Header="{Binding Data.ControlModule.AirCoil1BC.GetPatm_AltDisplay, Source={StaticResource proxy}}" Visibility="{Binding Data.ControlModule.AirCoil1BC.selPatm_AltValue,
Converter={StaticResource BoolToVisConverter}, Source={StaticResource proxy}, FallbackValue=Collapsed}" Binding="{Binding Path=ControlModule.AirCoil1BC.Patm_AltValue, Mode=TwoWay}"/>
<DataGridTextColumn Visibility="{Binding Data.ControlModule.AirCoil1BC.selESP,
Converter={StaticResource BoolToVisConverter}, Source={StaticResource proxy}, FallbackValue=Collapsed}" Header="Coil1 ESP" Binding="{Binding Path=ControlModule.AirCoil1BC.ESP, Mode=TwoWay}"/>
</DataGrid.Columns>
</DataGrid>
</Grid>
</StackPanel>
</DockPanel>
</Grid>
</DataTemplate>
</ResourceDictionary>
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>