My application is based on MVVM pattern. I want to access a RadGridView control/object, within Code-Behind of ResourceDictionary xaml to be able to use its Export method to export contents to an Excel file.
There's a UserControl XAML file which merges its resources with few individual ResourceDictionary xaml files. One of them is "ProdUnitResource.xaml" which has a DataTemplate control (x:key="ProdUnitVM"). This DataTemplate contains RadGridView control (Name="ProdUnitGrid". Cannot use x:key attribute since the parent control complains).
So, basically this is the structure -->
<UserControl> ProdUnit.xaml
<ResourceDictionary> ProdUnitResource.xaml (want to access "ProdUnitGrid" in code behind file ProdUnitResource.xaml.cs)
<DataTemplate x:key="ProdUnitVM">
<RadGridView Name="ProdUnitGrid">
I want to be export contents of this grid to an Excel file on click of an Hyperlink called "Export". So, I created a code-behind file for the ResourceDictionary xaml called "ProdUnitResource.xaml.cs" and was able to add following click event handler method to it.
private
void
Export_Click(
object
sender, System.Windows.RoutedEventArgs e)
{ ExportFormat format = ExportFormat.ExcelML;
var dialog =
new
System.Windows.Forms.SaveFileDialog();
dialog.DefaultExt =
"xls"
;
dialog.Filter = String.Format(
"Excel files (*.{0})|*.{0}|All files (*.*)|*.*"
, extension);
dialog.FilterIndex = 1;
if
(dialog.ShowDialog() == System.Windows.Forms.DialogResult.OK)
{
using
(Stream stream = dialog.OpenFile())
{
GridViewExportOptions exportOptions =
new
GridViewExportOptions();
exportOptions.Format = format;
exportOptions.ShowColumnHeaders =
true
;
ProdUnitGrid.Export(stream, exportOptions);
}
}
}
Click Event handling works fine but I am unable to directly use name of the RadGridView control "ProdUnitGrid" within code behind. It is not recognized. I want to access this RadGridView object so that I can call its Export method. Please share if you know how I can access it.