Using SfSpreadSheet with MVVM

Does SfSpreadSheet work with MVVM? I'm using the WPF 20.2.0.39 and .NET 6.

I have this so far in my view:

sfSkinManager:SfSkinManager.VisualStyle="Office2019White"
Workbook="{Binding Workbook}"/>


In my view model I have:

private IWorkbook? _workbook;
public IWorkbook? Workbook
{
get => _workbook;
private set => SetProperty(ref _workbook, value);
}
public void OnNavigatedTo(NavigationContext navigationContext)
{
foreach (object? view in _regionManager.Regions[RegionNames.ProjectContentRegionName].ActiveViews)
{
_regionManager.Regions[RegionNames.ProjectContentRegionName].Deactivate(view);
}


if (navigationContext.Parameters is NavigationParameters navParams)
{
DataSetFileName = (string) navParams[DataSetKey];
string workingPath = _illuminateProject.GetDataSetWorkingPath(DataSetFileName).GetAwaiter().GetResult(); // can't await this
Workbook = _excelEngine.Excel.Workbooks.Open(workingPath);
}
}


The workbook appears to be created correctly, but the binding doesn't appear to load the spreadsheet. Is SfSpreadSheet compatible with MVVM?


4 Replies

VS Vijayarasan Sivanandham Syncfusion Team July 25, 2022 02:55 PM UTC

Hi Walter,

SfSpreadsheet doesn't have support for data binding
. However,
you can import and export the datatable into SfSpreadsheet. Please find the UG link for your reference,
  


UG:
 https://help.syncfusion.com/wpf/spreadsheet/data-management

We suspect that the workingPath is not a valid path. So, the workbook doesn't appear in the spreadsheet. We have prepared the sample based on a provided code snippet from our end. Please find the sample in the attachment.

Please have a look at this sample and let us know if you have any concerns in this.

Regards,

Vijayarasan S


Attachment: SpreadSheetDemo_2349ab09.zip


WA Walter July 25, 2022 03:30 PM UTC

I'm confused by the response. You say it doesn't support data binding, but then you provided an MVVM sample where you're binding the workbook.

In my original code I can verify that workingPath is correct and that the Workbook object is created correctly.

The provided sample does work when the workbook is created in the view model constructor. I have attached a modified sample where instead of loading the workbook in the constructor, it happens on a command attached to a button. In this sample the spreadsheet does not load on the button press.

Since in my code the workbook is not created in the constructor, the modified sample is closer to the real behavior. It seems like if Workbook is bound to null (the initial value when the view is created), it does not update if the binding changes.


Attachment: SpreadSheetDemo_17cf0aed.zip


WA Walter July 25, 2022 08:36 PM UTC

I can get the load to work if using an attached property:


public class SfSpreadsheetProperties
{
public static IWorkbook GetWorkbook(DependencyObject obj)
{
return (IWorkbook) obj.GetValue(WorkbookProperty);
}


public static void SetWorkbook(DependencyObject obj, IWorkbook value)
{
obj.SetValue(WorkbookProperty, value);
}


public static readonly DependencyProperty WorkbookProperty = DependencyProperty.RegisterAttached("Workbook", typeof(IWorkbook), typeof(SfSpreadsheetProperties), new FrameworkPropertyMetadata(new PropertyChangedCallback(OnValueChangedCallBack)));


private static void OnValueChangedCallBack(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
if (d is SfSpreadsheet spreadsheet)
{
if (e.NewValue is IWorkbook workbook)
{
spreadsheet.Open(workbook);
}
}
}
}


VS Vijayarasan Sivanandham Syncfusion Team July 26, 2022 02:09 PM UTC

Hi Walter,


Please get in touch with us, if you require any further assistance. We will be glad to assist you.


Regards,

Vijayarasan S


Loader.
Up arrow icon