WPF ListView question

Go To StackoverFlow.com

0

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.

2009-06-16 14:56
by Teodor
I'm thinking now this is also a design problem, because i should have that information in the Role. Damn. Still the question remains, i can't wait to get over the noob phase with wpf.. - Teodor 2009-06-16 15:05


1

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

2009-06-16 15:07
by Jacob Adams
Thank you for answering, i just tried your first solution and it works - Teodor 2009-06-17 11:03
Ads