I am trying to solve this problem for days now and can´t seem to get anywhere close to solving it, which brings me here.
I am working on an MVVM WPF project and I am trying to populate my datagrid using data bindings.
The problem I am facing is that my datagrid does not update when I change my ObservableCollection mGießformenFinal. When I click the button on my GUI, the findCombinations() method returns items from my database to the collection. But even though the ObservableCollection is filled with items, they do not get updated on my datagrid. When I call findCombinations() in the constructor of my viewModel.cs the table is getting filled at start of the program.
There are plenty of things I already tried. I want to list a few here:
The final goal for the GUI is to provide a search field + button to get stuff from my database into the datagrid.
Does anyone know what I am missing here?
View.xaml
<Grid Grid.Row="2" Grid.Column="2" Margin="0,0,0,0">
<DataGrid Name="combinationJobOutput" AutoGenerateColumns="False" IsReadOnly="True" ItemsSource="{Binding mGießformenFinal, UpdateSourceTrigger=PropertyChanged, Mode=TwoWay}">
<DataGrid.Columns>
<DataGridTextColumn Header="Grundplatte" Binding="{Binding Grundplatte}"></DataGridTextColumn>
<DataGridTextColumn Header="Einlegeplatte" Binding="{Binding Einlegeplatte}"></DataGridTextColumn>
<DataGridTextColumn Header="Fuehrungsring" Binding="{Binding Fuehrungsring}"></DataGridTextColumn>
<DataGridTextColumn Header="Innenkern" Binding="{Binding Innenkern}"></DataGridTextColumn>
<DataGridTextColumn Header="Cupform" Binding="{Binding Cupform}"></DataGridTextColumn>
</DataGrid.Columns>
</DataGrid>
</Grid>
View.xaml.cs
public partial class AnsichtZwei : UserControl
{
private SuchenViewModel ViewModel { get; set; }
public AnsichtZwei()
{
InitializeComponent();
this.DataContext = new SuchenViewModel();
}
}
ViewModel.cs
class SuchenViewModel : ObservableObject
{
public ObservableCollection<ModularMold> mGießformenFinal { get; set; } = new ObservableCollection<ModularMold>();
public int productId { get; set; }
public ICommand searchCommand { get; set; }
public SuchenViewModel()
{
searchCommand = new SearchCommand(this);
}
public void findCombinations()
{
CombinationJob cj = new CombinationJob(productId);
cj.FiltereDiscDB();
this.mGießformenFinal = cj.KombiniereMGießformen();
}
}
SearchCommand.cs
internal class SearchCommand : ICommand
{
#region Constructors
public SearchCommand(SuchenViewModel viewModel)
{
_viewModel = viewModel;
}
private SuchenViewModel _viewModel;
#endregion Constructors
#region ICommand Members
public event System.EventHandler CanExecuteChanged
{
add { CommandManager.RequerySuggested += value; }
remove { CommandManager.RequerySuggested -= value; }
}
public bool CanExecute(object parameter)
{
return true;
}
public void Execute(object parameter)
{
_viewModel.findCombinations();
}
#endregion ICommand Members
}
the root of your issue is here
public ObservableCollection<ModularMold> mGießformenFinal { get; set; } = new ObservableCollection<ModularMold>();
public void findCombinations()
{
CombinationJob cj = new CombinationJob(productId);
cj.FiltereDiscDB();
this.mGießformenFinal = cj.KombiniereMGießformen();
}
this should be
public ObservableCollection<ModularMold> mGießformenFinal { get; } = new ObservableCollection<ModularMold>();
public void findCombinations()
{
CombinationJob cj = new CombinationJob(productId);
cj.FiltereDiscDB();
this.mGießformenFinal.Clear();
foreach(var item in cj.KombiniereMGießformen()){
this.mGießformenFinal.Add(item);
}
}
this is because when you call add, remove, clear or insert a CollectionChanged event is triggered that instructs the DataGrid to update its item source, when you directly replace the collection no event is raised and the view doesn't know anything has changed
you could also user a CollectionView to filter the Collection instead of actually editing it
another minor tweak
ItemsSource="{Binding mGießformenFinal, UpdateSourceTrigger=PropertyChanged, Mode=TwoWay}"
should be
ItemsSource="{Binding mGießformenFinal, Mode=OneWay}"
as at no point is the collection being directly modified by the DataGrid, the two way editing is by the columns