I'm trying to do a program to insert some materials like iron and steel and then show them from an interface developed in WPF.
EDIT :
public class MaterialRegistryPageVm : PageViewModel
{
private ObservableCollection<Material> _materials;
private readonly object _materialsLock = new object();
private Material _material;
private MaterialThickness _materialThickness;
private string _materialFilterFileName;
public ObservableCollection<Material> Materials
{
get
{
_materialCollectionView = CollectionViewSource.GetDefaultView(_materials);
_materialCollectionView.Filter = _DoesMaterialMatchFileNameFilter;
return _materials;
}
set => Set(ref _materials, value);
}
private ICollectionView _materialCollectionView;
private async void _AddMaterialExecution()
{
var material = new Material
{
TimeStamp = DateTime.Now
};
var dialog = new MaterialDialog(material);
var result = await DialogHost.Show(dialog, "RootDialog");
if (!result.Equals(true))
return;
material.Name = material.Name.Trim();
_AddMaterial(material);
lock (_materialsLock)
Application.Current.Dispatcher.BeginInvoke(new Action(() => this.Materials.Add(material)));
Material = material;
if (Mode == ContextMode.Online)
await _NotifyAddMaterialAsync(material);
}
I have a DataGrid which is populating data from ViewModel by asynchronous method. My DataGrid is:
<Grid Grid.Row="0" Margin="8">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*" />
<ColumnDefinition Width="Auto" />
</Grid.ColumnDefinitions>
<StackPanel Grid.Column="0" Orientation="Horizontal">
<Button Command="{Binding AddMaterialCommand, Mode=OneTime}" ToolTip="{DynamicResource Localization.ToolTip.CreateMaterial}" Height="48" Width="48" Style="{StaticResource MaterialDesignOutlinedButton}" materialDesign:ButtonAssist.CornerRadius="0" Padding="0" Margin="0">
<materialDesign:PackIcon Kind="Add" Height="24" Width="24" />
</Button>
<Button Command="{Binding RemoveMaterialCommand, Mode=OneTime}" ToolTip="{DynamicResource Localization.ToolTip.DeleteMaterial}" Width="48" Height="48" Style="{StaticResource MaterialDesignOutlinedButton}" materialDesign:ButtonAssist.CornerRadius="0" Padding="0" Margin="2 0 0 0">
<materialDesign:PackIcon Kind="Minus" Height="24" Width="24" />
</Button>
</StackPanel>
<TextBlock Grid.Column="1" Text="{DynamicResource Localization.Materials}" HorizontalAlignment="Right" VerticalAlignment="Center" Margin="32 0 16 0" Style="{StaticResource MaterialDesignHeadline5TextBlock}" />
</Grid>
On Application.Current.Dispatcher.BeginInvoke(new Action(() => this.Materials.Add(material)));
I've got this error: System.NotSupportedException: 'this type of collectionview does not support changes to its sourcecollection from a thread different from the Dispatcher thread'.
I've read these threads How do I update an ObservableCollection via a worker thread? and This type of CollectionView does not support changes to its SourceCollection from a thread different from the Dispatcher thread
but I don't understand so much, indeed I tried to change Materials.Add(material);
in Application.Current.Dispatcher.BeginInvoke(new Action(() => this.Materials.Add(material)));
as suggested but I have got the same error.
Errors : Stack Trace Stack Trace 2