locked
How to bind an object to a list of check boxes? RRS feed

  • Question

  • Have a list of check boxes below:

    <GridView x:Name="GvCategories" Margin="10,0,0,10">
                                <GridView.ItemTemplate>
                                    <DataTemplate>
                                        <Grid>
                                            <CheckBox x:Name="CbCategory" Content="{Binding Name}" Foreground="#FF0A0A0A" Checked="CbCategoryChecked" Unchecked="CbCategoryUnchecked"/>
                                        </Grid>
                                    </DataTemplate>
                                </GridView.ItemTemplate>
                                <GridView.ItemsPanel>
                                    <ItemsPanelTemplate>
                                        <WrapGrid MaximumRowsOrColumns="6"/>
                                    </ItemsPanelTemplate>
                                </GridView.ItemsPanel>
                            </GridView>

    These check boxes represents categories with ID's. I need to save the category ID's of the categories that was selected. A list of category objects is bound to the gridview from the page's viewmodel class. How do I bind the ID or category object to the check box?


    Wednesday, August 28, 2013 5:21 AM

Answers

  • Wanted the object (to get id property) that's currently bound to the instance of the check box in the checked event handler. I figured it out by grabbing the datacontext of frameworkelement which is the gridview.
    Thursday, August 29, 2013 5:09 AM

All replies

  • Binding the checkbox to a Boolean isn't difficult:

    IsChecked="{Binding CheckedValue}"

    But I'm not sure that's what you're asking for.  What do you want the end result to be?


    Matt Small - Microsoft Escalation Engineer - Forum Moderator
    If my reply answers your question, please mark this post as answered.

    NOTE: If I ask for code, please provide something that I can drop directly into a project and run (including XAML), or an actual application project. I'm trying to help a lot of people, so I don't have time to figure out weird snippets with undefined objects and unknown namespaces.

    Wednesday, August 28, 2013 7:26 PM
    Moderator
  • Wanted the object (to get id property) that's currently bound to the instance of the check box in the checked event handler. I figured it out by grabbing the datacontext of frameworkelement which is the gridview.
    Thursday, August 29, 2013 5:09 AM
  • Try to bind the object itself with CheckBox's Tag property. So you get get all the properties of object.
    Thursday, August 29, 2013 8:57 AM