none
Get selected check box value which are inside list box using MVVM

    Question

  • I hav a checkbox inside listbox.

    XAML:

    <ListBox x:Name="Product" ItemsSource="{Binding Product,Mode=TwoWay}"  ItemContainerStyle="{StaticResource ProdSelProdListBoxItemStyle}" Grid.Row="1" Style="{StaticResource ListBoxStyle}">
                <ListBox.ItemTemplate>
                    <DataTemplate>
                        <CheckBox x:Name="ProdCheckBox" IsChecked="{Binding IsChecked,Mode=TwoWay}" ClickMode="Press"  Content="" Margin="0" />
                    </DataTemplate>
                </ListBox.ItemTemplate>
    </ListBox>

    VM Code :

    public bool IsChecked
            {
                get
                {
                    return isChecked;
                }
                set
                {
                    isChecked = value;
                    RaisePropertyChanged("IsChecked");
                }
            }

    method Add :

     foreach (var item in ProductList)
                    {
                       if (item.IsChecked) /*getting false value all the time*/

                        {
                            productItem = new ProductDataContract();

                            productItem.Quantity = item.Quantity;
                            productItem.ProductDescr = item.Description;
                            productItem.ProductDisplayID = item.DisplayID;
                            productItem.ProductName = item.Name;

                             }
                    }

     

    On button click i want data of item whose check box is checked using MVVM

     

     Thank You

     

     

    Monday, March 28, 2011 3:01 AM

Answers

  • Hi,

    According to your decription, I have create a simple sample, check if there is someting you have missed:

    In VM code:

    Product class:

        public class Product : INotifyPropertyChanged
        {
            public int ProductDisplayID { get; set; }
            public string ProductName { get; set; }
            public string ProductDescr { get; set; }
            public int Quantity { get; set; }

            private bool isChecked;
            public bool IsChecked
            {
                get
                {
                    return isChecked;
                }
                set
                {
                    isChecked = value;
                    NotifyPropertyChanged("IsCheck");
                }
            }

            public event PropertyChangedEventHandler PropertyChanged;

            private void NotifyPropertyChanged(string propertyName)
            {
                if (PropertyChanged != null)
                {
                    PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
                }
            }
        }

     

    TestData class:

        public class TestData
        {
            public TestData()
            {
                Products = new ObservableCollection<Product>();
                for (int i = 1; i < 10; i++)
                {
                    Products.Add(new Product
                    {
                        ProductDisplayID = i,
                        ProductName = "Name " + i,
                        ProductDescr = "Product description " + i,
                        Quantity = i * 100,
                        IsChecked = false
                    });
                }
            }

            public ObservableCollection<Product> Products { get; set; }
        }

     

    In MainPage.xaml:

        <Grid x:Name="LayoutRoot" Background="White">
            <Grid.ColumnDefinitions>
                <ColumnDefinition Width="150"></ColumnDefinition>
                <ColumnDefinition Width="100"></ColumnDefinition>
                <ColumnDefinition Width="*"></ColumnDefinition>
            </Grid.ColumnDefinitions>
            <ListBox x:Name="Product" ItemsSource="{Binding Products,Mode=TwoWay}" Grid.Column="0">
                <ListBox.ItemTemplate>
                    <DataTemplate>
                        <CheckBox x:Name="ProdCheckBox" IsChecked="{Binding IsChecked,Mode=TwoWay}"
                                  ClickMode="Press"  Content="" Margin="0" />
                    </DataTemplate>
                </ListBox.ItemTemplate>
            </ListBox>
            <Button x:Name="btnAdd" Grid.Column="1" Height="30" Margin="5" Click="btnAdd_Click" />
            <sdk:DataGrid x:Name="Mydg" Grid.Column="2" />
        </Grid>

    In MainPage.xaml.cs:

            TestData data = new TestData();
            ObservableCollection<Product> ProductList = null;

            public MainPage()
            {
                InitializeComponent();

                this.DataContext = data;
                ProductList = data.Products;
            }


            private void btnAdd_Click(object sender, RoutedEventArgs e)
            {
                // Data source for data grid
                ObservableCollection<Product> dataSource = new ObservableCollection<Product>();

                foreach (var item in ProductList)
                {
                    if (item.IsChecked)
                    {
                        Product productItem = new Product();

                        productItem.Quantity = item.Quantity;
                        productItem.ProductDescr = item.ProductDescr;
                        productItem.ProductDisplayID = item.ProductDisplayID;
                        productItem.ProductName = item.ProductName;
                        productItem.IsChecked = item.IsChecked;

                        dataSource.Add(productItem); // Add item to datasource
                    }
                }

                Mydg.ItemsSource = dataSource;
            }

    Above code works well, please give that a try.

    Hope this helps.

     

    Regards

    Wednesday, March 30, 2011 11:40 PM

All replies

  • If you're getting IsChecked == false all the time regardless of the checked state of your checkbox, you should first take a look at whether you're even binding.  I'm going to guess that if you look at your output window you will find that there is a binding error.  I noticed that your model code is refering to a member called ProductList but the binding statement of your listbox refers instead to a property called Product.  I'd look here first.

    Monday, March 28, 2011 7:27 AM
  • Hi,

    Could you describe more about your scenarios?

    So that we can give you some choice.

    Monday, March 28, 2011 9:49 AM
  • If I follow your description right, you are using a single "isChecked" variable in your viewmodel. But in your xaml you are binding to the Product.isChecked variable. The DataContext of your listboxitems is not your viewmodel, it is a single product. Does your Product object have an "isChecked" property?

    Monday, March 28, 2011 4:42 PM
  • Hi,

    According to your decription, I have create a simple sample, check if there is someting you have missed:

    In VM code:

    Product class:

        public class Product : INotifyPropertyChanged
        {
            public int ProductDisplayID { get; set; }
            public string ProductName { get; set; }
            public string ProductDescr { get; set; }
            public int Quantity { get; set; }

            private bool isChecked;
            public bool IsChecked
            {
                get
                {
                    return isChecked;
                }
                set
                {
                    isChecked = value;
                    NotifyPropertyChanged("IsCheck");
                }
            }

            public event PropertyChangedEventHandler PropertyChanged;

            private void NotifyPropertyChanged(string propertyName)
            {
                if (PropertyChanged != null)
                {
                    PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
                }
            }
        }

     

    TestData class:

        public class TestData
        {
            public TestData()
            {
                Products = new ObservableCollection<Product>();
                for (int i = 1; i < 10; i++)
                {
                    Products.Add(new Product
                    {
                        ProductDisplayID = i,
                        ProductName = "Name " + i,
                        ProductDescr = "Product description " + i,
                        Quantity = i * 100,
                        IsChecked = false
                    });
                }
            }

            public ObservableCollection<Product> Products { get; set; }
        }

     

    In MainPage.xaml:

        <Grid x:Name="LayoutRoot" Background="White">
            <Grid.ColumnDefinitions>
                <ColumnDefinition Width="150"></ColumnDefinition>
                <ColumnDefinition Width="100"></ColumnDefinition>
                <ColumnDefinition Width="*"></ColumnDefinition>
            </Grid.ColumnDefinitions>
            <ListBox x:Name="Product" ItemsSource="{Binding Products,Mode=TwoWay}" Grid.Column="0">
                <ListBox.ItemTemplate>
                    <DataTemplate>
                        <CheckBox x:Name="ProdCheckBox" IsChecked="{Binding IsChecked,Mode=TwoWay}"
                                  ClickMode="Press"  Content="" Margin="0" />
                    </DataTemplate>
                </ListBox.ItemTemplate>
            </ListBox>
            <Button x:Name="btnAdd" Grid.Column="1" Height="30" Margin="5" Click="btnAdd_Click" />
            <sdk:DataGrid x:Name="Mydg" Grid.Column="2" />
        </Grid>

    In MainPage.xaml.cs:

            TestData data = new TestData();
            ObservableCollection<Product> ProductList = null;

            public MainPage()
            {
                InitializeComponent();

                this.DataContext = data;
                ProductList = data.Products;
            }


            private void btnAdd_Click(object sender, RoutedEventArgs e)
            {
                // Data source for data grid
                ObservableCollection<Product> dataSource = new ObservableCollection<Product>();

                foreach (var item in ProductList)
                {
                    if (item.IsChecked)
                    {
                        Product productItem = new Product();

                        productItem.Quantity = item.Quantity;
                        productItem.ProductDescr = item.ProductDescr;
                        productItem.ProductDisplayID = item.ProductDisplayID;
                        productItem.ProductName = item.ProductName;
                        productItem.IsChecked = item.IsChecked;

                        dataSource.Add(productItem); // Add item to datasource
                    }
                }

                Mydg.ItemsSource = dataSource;
            }

    Above code works well, please give that a try.

    Hope this helps.

     

    Regards

    Wednesday, March 30, 2011 11:40 PM
  • But as per my understanding it is against the principle of MVVM as code is written behind xaml.cs. Pls correct me if I'm wrong
    Monday, January 29, 2018 8:50 AM