0
votes

I am using a ListView in wpf mvvm pattern whose SelectedItem binding is done to the ViewModel. The problem what I am facing is as soon as I check the checkbox, The SelectedItem binding is not working immediately. It work only when I click again somewhere outside the checkbox and its respective content.

My ListView is like this:

<Grid>
            <Grid.Resources>
                <DataTemplate x:Key="checkboxHeaderTemplate">
                    <CheckBox IsChecked="{Binding Path=DataContext.AllSelected,RelativeSource={RelativeSource AncestorType=UserControl },Mode=TwoWay}">
                    </CheckBox>
                </DataTemplate>

                <DataTemplate x:Key="CheckBoxCell">
                    <!--<CheckBox Checked="CheckBox_Checked" />-->
                    <CheckBox IsChecked="{Binding Path=IsSelected, Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}" >
                    </CheckBox>
                </DataTemplate>

                <DataTemplate x:Key="TextCell">
                    <TextBlock  Text="Usecasename">
                    </TextBlock>
                </DataTemplate>

                <DataTemplate x:Key="ButtonCell">
                    <Button  Content="{Binding Path=UsecaseName, Mode=TwoWay}" >
                    </Button>
                </DataTemplate>
            </Grid.Resources>

            <ListView SelectedItem="{Binding SelectedSection}" ItemsSource="{Binding Path=UsecaseListItems}" >
                <ListView.View>
                    <GridView>
                        <GridView.Columns>

                            <GridViewColumn HeaderTemplate="{StaticResource checkboxHeaderTemplate}"
                                        CellTemplate="{StaticResource CheckBoxCell}" Width="auto">
                            </GridViewColumn>

                            <GridViewColumn HeaderTemplate="{StaticResource TextCell}"
                                        CellTemplate="{StaticResource ButtonCell}" Width="auto">
                            </GridViewColumn>
                        </GridView.Columns>
                    </GridView>
                </ListView.View>
            </ListView>
        </Grid>

The HomeViewModel with which I am binding the Selected itm of List View is like this:

private UseCase _selectedSection;
        public UseCase SelectedSection
        {
            get { return _selectedSection; }
            set
            {
                _selectedSection = value;
                if (this.SelectedSection.UsecaseName == ("CCS01") && (this.SelectedSection.IsSelected == true))
                {
                    this.ContentWindow = new CCS01();
                }
               else if (this.SelectedSection.UsecaseName == ("CCS02") && (this.SelectedSection.IsSelected == true))
                {
                    this.ContentWindow = new CCS02();
                }
                else if (this.SelectedSection.UsecaseName == ("ECS52") && (this.SelectedSection.IsSelected == true))
                {
                    this.ContentWindow = new ECS52();
                }
                else
                    this.ContentWindow = new Default();
                OnPropertyChanged("SelectedSection");
            }
        }

and The UseCase class is this:

public class UseCase: BaseNotifyPropertyChanged
    {
        public string UsecaseName { get; set; }



        private bool _IsSelected;
        public bool IsSelected
        {
            get { return _IsSelected; }
            set
            {
                _IsSelected = value;
                OnPropertyChanged("IsSelected");

            }
        }




    }

Please suggest what correction should I do so that, It should hit the binding directly as I check the Checkboxes.

1
Here is a good example by Rachel Lim that demonstrates exactly what your trying to achieve... rachel53461.wordpress.com/2011/12/18/navigation-with-mvvm-2Monty

1 Answers

0
votes

You should change the checkbox binding of the CheckBoxCell to something like this :

<DataTemplate x:Key="CheckBoxCell">
                <!--<CheckBox Checked="CheckBox_Checked" />-->
                <CheckBox IsChecked="{Binding Path=IsSelected, 
                          RelativeSource={RelativeSource FindAncestor, 
                  AncestorType={x:Type ListViewItem}}, 
                          Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}" >
                </CheckBox>
            </DataTemplate>

And for this work you need to put SelectionMode to Single

 <ListView SelectedItem="{Binding SelectedSection}" 
ItemsSource="{Binding Path=UsecaseListItems}" SelectionMode="Single">

And do this in your viewmodel

private UseCase _selectedSection;
        public UseCase SelectedSection
        {
            get { return _selectedSection; }
            set
            {
                DeSelectAll();
                _selectedSection = value;
                _selectedSection.IsSelected = true;
                OnPropertyChanged("SelectedSection");
            }
        }

        private void DeSelectAll()
        {
            foreach (var item in UsecaseListItems)
            {
                item.IsSelected = false;
            }
        }