2

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
        }

    }

1 Answer 1

2

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.