8

I have a ListView in WPF application with a CheckBox.

I want to save the values of all Checked rows in a WPF List ...

How can I achieve this?

My ListView

<ListView x:Name="listViewChapter" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" BorderThickness="0" SelectionMode="Single" Height="100" Margin="22,234,17,28" Grid.Row="1">
    <ListView.ItemTemplate>
            <DataTemplate>
                <StackPanel Orientation="Horizontal" HorizontalAlignment="Left" VerticalAlignment="Center" >
                    <Label Name="lblChapterID" VerticalAlignment="Center"  Margin="0" Content="{Binding ChapterID}" Visibility="Hidden" />
                    <CheckBox Name="chkChapterTitle" VerticalAlignment="Center" Margin="0,0,0,0" Content="{Binding ChapterTittle}" Checked="chkChapterTitle_Checked" />
                </StackPanel>
            </DataTemplate>
    </ListView.ItemTemplate>
</ListView>

2 Answers 2

12

You can bind the IsChecked property directly to IsSelected of the ListViewItem. Use RelativeSource to bind to the element.

IsChecked="{Binding RelativeSource={RelativeSource AncestorType=ListViewItem},Path=IsSelected}"

Now if you use SelectionMode=Multiple for the ListView, you can pull the checked items directly using SelectedItems.

var chapters = new List<Chapter>();
foreach (var item in listViewChapter.SelectedItems)
    users.Add((Chapter)item);
Sign up to request clarification or add additional context in comments.

2 Comments

i have Added IsChecked="{Binding RelativeSource={RelativeSource AncestorType=ListViewItem},Path=IsSelected}" in ListView.. Now How i can Get Checked rows values ..
After Adding this in CheckBox Only Single CheckBox checked, I want multilple checkBox Checked....
0

You should consider using the MVVM pattern for your WPF application, and if you're going to use MVVM then you'll want an MVVM framework.

Then, it would be a case of creating a type that represents your databound object (e.g Book), then having a collection of this type on your view model (e.g. ObservableCollection<Book> Books).

You would then bind a Selected boolean property for example on your Book type to the CheckBox's IsChecked property in your ListBox ItemTemplate.

<CheckBox IsChecked="{Binding Selected}" />

You may not want to polute your domain object (Book) with properties purely used for the UI (Selected), so you could create a BookViewModel type which augments the Book type and reshapes the object purely for the purposes of the view.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.