tags:

views:

362

answers:

1

Hello guys, this is my first question here. I'm getting started with WPF and i am stuck. Here is the problem: I have a ListView as following:

<UserControl.Resources>
    <DataTemplate x:Key="FirstCell">
        <StackPanel Orientation="Horizontal">
            <CheckBox Margin="2"></CheckBox>
        </StackPanel>
    </DataTemplate>
</UserControl.Resources>

<ListView Name="lvRights">
    <ListView.View>
        <GridView>
            <GridViewColumn Width="100" Header="Select" CellTemplate="{StaticResource FirstCell}"/>
            <GridViewColumn Width="200" Header="Right" DisplayMemberBinding="{Binding Path=Name}" />
        </GridView>       
    </ListView.View>
</ListView>

I am binding the list to a collection of "Roles", which have only Id and Name. I am using that DataTemplate to display a checkbox in the first column.

And here is the question:

How can I know at runtime whether the user checked one of the checkboxes? In the .Items property of the listview i have the Roles, but i cannot get any information about the first column.

I have the feeling this is SOO simple, but somehow i am missing the answer.

10x in advance.

+1  A: 

You can either

1) add a click handler to the check box in the template. In the code behind you can cast the DataContext of the checkbox back to a Role to figure out which one it is.

2) You can add some sort of boolean property to your Role class. You can then bind the IsChecked property of the checkbox to this boolean property. You may need a binding converter to converter between the boolean and the is checked property

Jacob Adams
Thank you for answering, i just tried your first solution and it works!
Teodor