How to get Validated String Values ​​in ListView in WPF

I have a ListView in a WPF application with CheckBox .

I want to keep the values ​​of all checked lines 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> 
+7
source share
2 answers

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

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

Now, if you use SelectionMode=Multiple for a ListView, you can directly wrap the marked items using SelectedItems .

 var chapters = new List<Chapter>(); foreach (var item in listViewChapter.SelectedItems) users.Add((Chapter)item); 
+6
source

You should consider using the MVVM pattern for your WPF application, and if you intend to use MVVM, you will need the MVVM framework .

Then this will be the case of creating a type that represents your data binding object (for example, Book ), and then a set of this type in your view model (for example, ObservableCollection<Book> Books ).

Then you would bind the Selected boolean boolean property, for example, in your Book type, to the CheckBox IsChecked property in the ItemBox ListBox.

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

You may not want to declare your domain object ( Book ) properties that are purely used for the user interface ( Selected ), so you can create a BookViewModel type that adds a Book type and modifies the object for presentation purposes only.

0
source

All Articles