Goal
CheckBoxColumn TwoWay binding for access of user input.
Problem description
1. Empty table (so object instance is not found)
2. How does MVVM know which instance of an object to use? Is there any debug/help option for correct typing?
.xaml
<DataGrid x:Name="bGrid" ItemsSource="{Binding
Source=window.obColl_B,
UpdateSourceTrigger=PropertyChanged, Mode=TwoWay,
Path=BindsDirectlyToSource}" >
<DataGrid.Columns>
<DataGridCheckBoxColumn Width="0.25*" Header="Check"
Binding="{Binding berCheck}" IsReadOnly="False" />
<DataGridTextColumn Width="0.5*" Header="Beruf"
Binding="{Binding berName}" IsReadOnly="True" />
</DataGrid.Columns>
</DataGrid>
c#
public class obsColl : ObservableCollection<bEntry>
{
public obsColl(List<string> berList, List<string> bActive) : base()
{
for (int i = 0; i < berList.Count; i++)
{
if (bActive.Contains(berList[i]))
Add(new bEntry(true, berList[i]));
else
Add(new bEntry(false, berList[i]));
}
}
}
public class bEntry
{
private bool _berCheck;
private string _berName;
public bEntry(bool check, string name)
{
this._berCheck = check;
this._berName = name;
}
public bool berCheck
{
get { return _berCheck; }
set { _berCheck = value; }
}
public string berName
{
get { return _berName; }
set { _berName = value; }
}
}
public partial class window : Window
{
obsColl obColl_B;
public AuswahlBerufWindow(List<string> berList, List<string> berActive)
{
obColl_B = new obsColl(cpy_berList, cpy_berActive);
//so how does MVVM find the correct object to bind?
InitializeComponent();
...
}
}
// obColl_B automatically changes due to user input
As far as I understood. ObservableCollection handles IPropertyChanged and INotifyChanged for me, so I should be able to bind that with user input using Mode=TwoWay. However even single bindings fail. The idea I did use was from https://blogs.u2u.be/diederik/post/Codeless-two-way-data-binding-to-a-WPF-DataGrid
However what I do not quite understand is how the correct element shall be assigned for the MVVM to use. Most examples do use static input, which is of no help. From what I did read is that I need to give the .xaml interpreter objects of according type (ObservableCollection), but I dont know how to get the values back via Mode=TwoWay then.
I tried another approach for writing the DataTable by hand without the binding, but that way (defining only type,header and readonly) only the count of checkboxes were added without the content. Interestingly enough that did not fail with Exceptions.
Update 1
Error 1. Binding is wrong/program fails with "EditItem is not allowed for this View", and maybe fixed, if Path+Mode+UpdateSourceTrigger not set. However that looks 'fixed'.
Update 2
The mistake should lie in the incorrect object source.