I have an "import xlsx" function on my app's main window which return an object with import result info, and i launch this function from a task like this
var scheduler = TaskScheduler.FromCurrentSynchronizationContext();
System.Threading.Tasks.Task.Run(() =>
{
result = XlsxImport.Import(openFileDialog.FileName);
return result;
})
.ContinueWith(r =>
{
if (r.Result.Errors.Count > 0)
{
if (MessageBox.Show("Import finished with " + r.Result.Errors.Count.ToString() + " errors. An error list will be shown.", "Errors occurred during import", MessageBoxButton.OKCancel) == MessageBoxResult.OK)
{
ImportErrorsView importErrorWindow = new ImportErrorsView(r.Result);
importErrorWindow.Show();
}
}
else
{
MessageBox.Show("Import succesfully completed");
}
}, scheduler);
importErrorWindow is a simply window with a grid where i bind the error list.
BUT when the importErrorWindow is shown, it's grid contains the right number of row but all of them are empty, and in console i can see nothing but binding errors, while i'm 100% sure that bindings are corrects (i checked debugging it step by step)
I also tried to remove colum bindings and set the AutoGenerateColums grid property to True, but the result is the same: correct rows number but all blank
why is this happening? is it related with the task?
UPDATE Here's the code of my window
<Window x:Class="MyApp.Module.WBS.Views.ImportErrorsView"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:telerik="http://schemas.telerik.com/2008/xaml/presentation"
Title="ImportErrorsReport"
xmlns:vm="clr-namespace:MyApp.Module.WBS.ViewModels">
<Window.Resources>
<vm:WBSImportErrorsViewModel x:Key="WBSImportErrorsViewModel" />
</Window.Resources>
<Grid x:Name="MainGrid" DataContext="{StaticResource WBSImportErrorsViewModel}">
<telerik:RadGridView Name="ErrorList" CanUserFreezeColumns="False" CanUserInsertRows="False" GroupRenderMode="Flat"
AutoGenerateColumns="False" ItemsSource="{Binding ImportErrors}"
ShowGroupPanel="False" EnableRowVirtualization="True" EnableColumnVirtualization="False"
FilteringMode="FilterRow" RowIndicatorVisibility="Collapsed" >
<telerik:RadGridView.Columns>
<telerik:GridViewDataColumn Header="Line" DataMemberBinding="{Binding XlsxLine}" Width="60" />
<telerik:GridViewDataColumn Header="ErrorMessage" DataMemberBinding="{Binding ErrorMessage}" Width="400" />
<telerik:GridViewDataColumn Header="Exception Message" DataMemberBinding="{Binding Exception.Message}" Width="300" />
<telerik:GridViewDataColumn Header="Exception InnerException" DataMemberBinding="{Binding Exception.InnerException}" Width="400" />
</telerik:RadGridView.Columns>
</telerik:RadGridView>
</Grid>
</Window>
namespace MyApp.Module.WBS.Views
{
public partial class ImportErrorsView : Window
{
ImportResult ir;
public ImportErrorsView()
{
InitializeComponent();
}
public ImportErrorsView(ImportResult ir)
{
InitializeComponent();
((WBSImportErrorsViewModel)this.MainGrid.DataContext).ImportErrors = new ObservableCollection<ImportError>(ir.Errors);
}
}
}
the ViewModel:
namespace MyApp.Module.WBS.ViewModels
{
public class WBSImportErrorsViewModel : ViewModelBase
{
private ObservableCollection<ImportError> _importErrors;
public ObservableCollection<ImportError> ImportErrors
{
get
{
return this._importErrors;
}
set
{
this._importErrors = value;
this.OnPropertyChanged(() => this.ImportErrors);
}
}
}
}
and the object
public class ImportResult
{
public List<ImportError> Errors;
public ImportResult()
{
//other non-relevant fields
Errors = new List<ImportError>();
}
}
public class ImportError
{
public int? XlsxLine;
public string ErrorMessage;
public Exception Exception;
public ImportError(int? xlsxLine, string errorMessage, Exception exception)
{
XlsxLine = xlsxLine;
ErrorMessage = errorMessage;
Exception = exception;
}
}
The class ImportError
should have the fields XlsxLine, ErrorMessage, Exception
as properties. You should also implement INotifyPropertyChange
if you want the individual properties to notify the grid on changes occurring.