jeudi 16 mars 2017

WPF Binding CheckBox IsChecked to ItemsSource DataModel Failing to See Model

I have a WPF form using MVVM. Within that WPF window I have a reference to my data context:

DataContext="{x:Static modelView:MasterAutoSyncModelView.Instance}"

This model view contains an observable collection

public ObservableCollection<AutoSyncJob> Jobs { get; set; }

In the window I have a list view. This list view contains a list of Jobs. The Jobs class looks like this:

public sealed class AutoSyncJob : ObservableModel
    {
        private string _jobName;
        public string JobName
        {
            get { return _jobName; }
            set
            {
                _jobName = value;
                OnPropertyChanged("JobName");
            }
        }

        private bool _isActive;
        public bool IsActive
        {
            get { return _isActive; }
            set
            {
                _isActive = value;
                OnPropertyChanged("IsActive");
            }
        }
    }

The list should have the checkboxes checked for a job that has its IsActive property set to true.

<ListView x:Name="lvJobs" HorizontalAlignment="Left" Height="628" Margin="30,62,0,0" ItemsSource="{Binding Jobs}" 
                  SelectedItem="{Binding SelectedJob, Mode=TwoWay}" VerticalAlignment="Top" Width="205">
            <ListView.View>
                <GridView>
                    <GridView.Columns>
                        <GridViewColumn Header="Active" Width="50">
                            <GridViewColumn.CellTemplate>
                                <DataTemplate>
                                    <CheckBox IsChecked="{Binding IsActive, Mode=TwoWay}"/>
                                </DataTemplate>
                            </GridViewColumn.CellTemplate>
                        </GridViewColumn>
                        <GridViewColumn DisplayMemberBinding="{Binding JobName}" Header="Job Name" Width="150">

                        </GridViewColumn>
                    </GridView.Columns>
                </GridView>
            </ListView.View>
        </ListView>

In the constructor for my test code I have two jobs. The first job has its IsActive set to True. The second has it set to False.

When loading the window, both checkboxes are empty even though the IsActive is set to True.

Visual Studio complains that IsActive cannot be found. Its looking at my view model for the IsActive property, not the Job from the Jobs ItemsSource.

Rewriting the code to say

<CheckBox IsChecked="{Binding Jobs/IsActive, Mode=TwoWay}"/>

Removes this complaint, but the window still has no selected checkbox on load.

How do you bind the IsChecked property to the IsActive boolean on the AutoSyncJob bound to that row from the Jobs ItemsSource so that it shows up checked or unchecked depending on the underlying model?

Second question: checking or unchecking the active box in the ListView does not trigger code in the model's setter, even though I have the mode set to TwoWay. Therefore if I uncheck or check the box, I would expect the model to have its IsActive property modified... but there is no communication. I suspect this is because again the DataTemplate is not really bound to the model item.




Aucun commentaire:

Enregistrer un commentaire