0
votes

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.

1

1 Answers

0
votes

In your ViewModel you have to introduce a property of the type ObservableCollection.

In your XAML you have to set the Datacontext to either the whole ViewModel or only to the property.

Example for binding the MainViewModel to your MainWindow.xaml

DataContext="{Binding Main, Source={StaticResource Locator}

MVVM knows what to use because of the ViewModelLocator

    public MainViewModel Main
    {
        get
        {
            return ServiceLocator.Current.GetInstance<MainViewModel>();
        }
    }

I would suggest using MVVM Light. MVVM Light offers you quite good basic implementation of the MVVM pattern.