views:

80

answers:

1

Hi,

I have one usercontrol where am showing the main categories as buttons from the database. By clicking on Main Category button, a new user control is getting opened with list of items in that respective category clicked. I am binding the Item Names of that category in a xaml using TexbBlock and also am showing the checkboxes with each Items to select. I want to get the values of checkboxes after selecting them and clicking on the Done button. Checkbox values should be the items ID in the database. How to get the selected checkbox values.

Here is the xaml for the same:

                        <!--<Button  Content="{Binding CategoryName}" Margin="5"/>-->
                        <TextBlock Text="{Binding SubCategoryName}" HorizontalAlignment="Center" VerticalAlignment="Top"  />                         
                        <CheckBox Grid.Row="3" />
                    </StackPanel>
                </DataTemplate>
            </ItemsControl.ItemTemplate>

Kindly suggest?

Thanks,

Tarun

+1  A: 

I think what you need to do, is have a boolean property on you ViewModel that is called "Checked" (or something similar) then bind it to your checkbox

IsChecked="{Binding Checked, Mode=TwoWay}"

The "Mode=TwoWay" will mean that when you Check the CheckBox, the binding will automatically update the property on your ViewModel.

Hope this helps

Ben
@Ben. How to keep the selected checkbox values in collection after clicking on the button. Kindly Suggest?
Tarun
@Tarun, if you have the checkbox bound to your view model, you can simply iterate through the collection and say "If MyViewModel.Checked Then...". Make sense?
Ben
@Ben. Yes. Will try n let u know. Thanks
Tarun
@Ben. I have tried that one but didnt get success. Please elaborate.Thanks.
Tarun
What do you mean by no success? Can you set a breakpoint on the Set of the Checked property, and make sure that when you check the checkbox, it sets the property value correctly? Do you actually have a collection of this ViewModel to iterate through?
Ben
Yes I have the collection CategoriesItems in my viewmodel and IsSelected property in the Model. If I checked some checkboxes,am getting true values by setting the breakpoint. How to loop through the collection in viewmodel and storing those selected values in database. Kindly Suggest?
Tarun
Here is my collection:private ObservableCollection<Model.HouseholdSubCategories> _categoriesItems; public ObservableCollection<Model.HouseholdSubCategories> CategoriesItems { get { return _categoriesItems; } set { _categoriesItems = value; RaisePropertyChanged("CategoriesItems"); } }
Tarun
@Ben. Finally Got it.Thanks.
Tarun