I'm fairly new to WPF and was wondering whether a possibility exists to simply export a WPF DataGrid to a csv file. I've tried using reflections to get the values needed, though that works to some extent I was wondering whether it's possible with an attached property to get the displayed values, these do not necessarily correspond to the values of the item source. The below attached property works as long as I use a static string, or a static resource of a string etc. If I try to use the columns bindings I just get a the default string.empty
public static readonly DependencyProperty ExportStringProperty =
DependencyProperty.RegisterAttached("ExportString", //name of attached property
typeof(string), //type of attached property
typeof(ExportBehaviour), //type of this owner class
new PropertyMetadata(string.Empty)); //the default value of the attached property
public static string GetExportString(DataGridColumn column)
{
return (string)column.GetValue(ExportStringProperty);
}
public static void SetExportString(DataGridColumn column, string value)
{
column.SetValue(ExportStringProperty, value);
}
Is there a similar way to get the binding value from xaml in a way like:
<DataGridTextColumn Header="Name" Binding="{Binding (datagridexcel:Product.Name)}" datagridexcel:ExportBehaviour.ExportString="{Binding (datagridexcel:Product.Name)}"/>
as said, the above earlier works for a static typed string and not for the binding. It has to be said that working with the item source in this case should be avoided and the only thing I'm interested in is the datagrid and the values shown there.