Click here to Skip to main content
15,881,089 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Binding is not working and ui is not updating.i am following MVVM pattern.
Xaml

<ListView Grid.Row="1" Name="listbox" ItemsSource="{Binding Items}"   Margin="5,5,5,5" Width="Auto" >

                <ListView.View >
                    <GridView  >

                        <GridViewColumn Width="80"  >
                        <GridViewColumn.Header >
                            <CheckBox  x:Name="cbSelectAll" IsChecked="{Binding ModelDetails.IsChecked}"  Command="{Binding cbSelectAll_Checked}"/>
                        </GridViewColumn.Header>

                            <GridViewColumn.CellTemplate>
                                <DataTemplate>
                                    <StackPanel Orientation="Horizontal">
                                    <WrapPanel x:Name="Layout"   >
                                       <CheckBox  IsChecked="{Binding IsSelected, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" />
                                    </WrapPanel>
                                </StackPanel>
                                </DataTemplate>
                            </GridViewColumn.CellTemplate>
                        </GridViewColumn>
                      </ListView.View >
                    </GridView  >  
                  </ListView>

Model.cs

class ViTypeModel : INotifyPropertyChanged
    {
        private bool _isSelected;
        private bool _isChecked;
        //Define an event based on delegates
        public event PropertyChangedEventHandler PropertyChanged;
        //Raise the event
        public void OnPropertyChanged(string propertyName)
        {
            if (PropertyChanged != null)
            {
                //publishing the event in current classs
                PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
            }
        }
public bool IsChecked
        {
            get
            {
                return _isChecked;
            }
            set
            {
                _isChecked = value;
                OnPropertyChanged(nameof(IsChecked));
            }
        }

        public bool IsSelected
        {
            get
            {
                return _isSelected;
            }
            set
            {
                _isSelected = value;
                OnPropertyChanged(nameof(IsSelected));
            }
        }


What I have tried:

ViewModel


private ViTypeModel _ModelDetails;


        public ViTypeModel ModelDetails
        {
            get
            {
                return _ModelDetails;
            }
            set
            {
                _ModelDetails = value;
            }
        }




on checkbox click event below function will execute

public void SelectAllBtnExecute(object param)
      {

          foreach (var selectedItems in Items)
          {
              ModelDetails.IsSelected = ModelDetails.IsChecked;
          }

      }
Posted
Updated 18-Jan-21 22:00pm
v2
Comments
Richard Deeming 19-Jan-21 4:42am    
Check the output window in Visual Studio for data binding errors.
George Swan 19-Jan-21 5:28am    
Try adding OnPropertyChanged(nameof(ModelDetails) to your ModelDetails property

1 solution

Seems you have not marked your ViewModel with INotifyPropertyChanged

Thus, your property is also not making use of interface making sure the changes can reflect.
Please have a look at this to refer:
How to: Implement Property Change Notification - WPF .NET Framework | Microsoft Docs[^]
INotifyPropertyChanged.PropertyChanged Event (System.ComponentModel) | Microsoft Docs[^]

Try out.
 
Share this answer
 
Comments
john321a 19-Jan-21 3:57am    
INotifyPropertyChanged already added.but not working!
Sandeep Mewara 19-Jan-21 4:07am    
It would be difficult to trace such thing as is. Would suggest you to use tool like snoop to first assess the bindings are in place as expected. Tool: GitHub - snoopwpf/snoopwpf: Snoop - The WPF Spy Utility[^]

Once they are and INotifyPropertyChanged interface is referred, it should work out. Other alternatives to try would be to start with some textbox on the page, then in grid and so to see what is working for you and move step wise.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900