nikhil
nikhil

Reputation: 1748

Binding CheckBox Command

I have a checkbox for each row in the listview. I want to bind the Checkbox OnClick to my command in the view model. But its not binding to my Command. What should I do? Below is my xaml and Viewmodel

Xaml:

<Grid>
        <ListView Name="lstdemo" ItemsSource="{Binding obcollection}">
            <ListView.View>
                <GridView>
                    <GridView.Columns>
                        <GridViewColumn>
                            <GridViewColumn.CellTemplate>
                                <DataTemplate>
                                    <CheckBox Name="chk" IsChecked="{Binding IsChecked,Mode=TwoWay,NotifyOnTargetUpdated=True}" Command="{Binding Path=UpdateCommand}" />
                                </DataTemplate>
                            </GridViewColumn.CellTemplate>
                        </GridViewColumn>
                        <GridViewColumn DisplayMemberBinding="{Binding Name}" Header="Name" />
                    </GridView.Columns>
                </GridView>
            </ListView.View>

        </ListView>
    </Grid>

ViewModel:

public class MyViewModel
    {
        private List<Demo> lstdemo;
        private ObservableCollection<Demo> Obcollection;
        private SampleDb db;
        public MyViewModel()
        {
            db = new SampleDb();
            lstdemo = db.Demoes.ToList();
            Convert();

        }

        public void Convert()
        {
            Obcollection = new ObservableCollection<Demo>(lstdemo);
        }

        public ObservableCollection<Demo> obcollection
        {
            get { return Obcollection; }
            set { Obcollection = value; }
        }

        private ICommand mUpdater;
        public ICommand UpdateCommand
        {
            get
            {
                if (mUpdater == null)
                    mUpdater = new Updater();
                return mUpdater;
            }
            set
            {
                mUpdater = value;
            }
        }

        public class Updater : ICommand
        {
            #region ICommand Members

            public bool CanExecute(object parameter)
            {

                return true;
            }

            public event EventHandler CanExecuteChanged;

            public void Execute(object parameter)
            {
                if (parameter == null)
                {
                    MessageBox.Show("Hello");
                }

            }

            #endregion
        }

    }

Upvotes: 2

Views: 4480

Answers (1)

King King
King King

Reputation: 63387

The DataContext in DataTemplate is implicitly the current item (of the ListView). So in this case you have to set the Source (or RelativeSource) for your Binding explicitly. You can use the RelativeSource to find the parent ListView like this:

<CheckBox Name="chk" IsChecked="{Binding IsChecked,
                                 Mode=TwoWay,NotifyOnTargetUpdated=True}" 
          Command="{Binding Path=DataContext.UpdateCommand, 
                    RelativeSource={RelativeSource AncestorType=ListView}}"/>

Note about the Path change. The source now is the ListView, so the path to the UpdateCommand is DataContext.UpdateCommand.

Upvotes: 2

Related Questions