Khonan
Topic Author
Posts: 24
Joined: 23 Jan 2019, 10:18

ListView with CheckBox

27 Feb 2019, 15:38

Hi,

Anyone having a way to retrieve the listview raw at the moment you check a checkbox ?

When you check the CheckBox, the listview row is not selected (I tried all SelectionModes) so no way to get the SelectedItem or SelectedIndex.

I'm running out of ideas... :p

I have the following XAML code:
                <ListView x:Name="itListViewFiles"  SelectionMode="Single">
                    <ListView.View>
                        <GridView>
                            <GridView.ColumnHeaderContainerStyle>
                                <Style TargetType="{x:Type GridViewColumnHeader}">
                                    <Setter Property="Visibility" Value="Hidden"/>
                                </Style>
                            </GridView.ColumnHeaderContainerStyle>
                            <GridViewColumn Header="">
                                <GridViewColumn.CellTemplate>
                                    <DataTemplate>
                                        <CheckBox Margin="0" VerticalAlignment="Center" IsChecked="{Binding IsChecked}" Checked="OnCheck"></CheckBox>
                                    </DataTemplate>
                                </GridViewColumn.CellTemplate>
                            </GridViewColumn>
                            <GridViewColumn Header="{StaticResource locRevitFileName}">
                                <GridViewColumn.CellTemplate>
                                    <DataTemplate>
                                        <TextBlock Margin="0" Text="{Binding Text}"></TextBlock>
                                    </DataTemplate>
                                </GridViewColumn.CellTemplate>
                            </GridViewColumn>
                        </GridView>
                    </ListView.View>
                </ListView>

Tags:
 
Khonan
Topic Author
Posts: 24
Joined: 23 Jan 2019, 10:18

Re: ListView with CheckBox

28 Feb 2019, 16:01

Still no solution to this problem.

In the mean time (and if someone's got the same problem) I've done the following:

My listview items are : public class CheckBoxListViewItem : INotifyPropertyChanged

I just added an ugly static to the class (public static CheckBoxListViewItem m_LastItemChanged = null;) Which is updated on RaisePropertyChanged...
 
User avatar
sfernandez
Site Admin
Posts: 1911
Joined: 22 Dec 2011, 19:20

Re: ListView with CheckBox

28 Feb 2019, 16:39

Have you tried to search up the tree (using VisualTreeHelper.GetParent) looking for the ListViewItem?
You will receive the CheckBox being clicked on the OnCheck event handler.

Anyway, I think taking an MVVM approach to this will simplify things. You could bind a property of your viewmodel to the CheckBox.IsChecked, when the setter gets triggered do the required logic, and update other properties that will be bound to the ListViewItem to reflect the change.

Do you think any of this could work in your scenario?
 
Khonan
Topic Author
Posts: 24
Joined: 23 Jan 2019, 10:18

Re: ListView with CheckBox

28 Feb 2019, 17:53

Have you tried to search up the tree (using VisualTreeHelper.GetParent) looking for the ListViewItem?
You will receive the CheckBox being clicked on the OnCheck event handler.
Didn't try this as I usualy avoid using VisualTreeHelper on List. I remember cases where with DatagridView VisualTreeHelper wasn't of any help due to virtualisation of items (to manage large list of items).
Anyway, I think taking an MVVM approach to this will simplify things. You could bind a property of your viewmodel to the CheckBox.IsChecked, when the setter gets triggered do the required logic, and update other properties that will be bound to the ListViewItem to reflect the change.
That's what I've done in the end, but I was pointing out the difference with wpf where the click on the checkbox trigger the selection of the row. Therefore you only have to get the SelectedItem (otherwise you only have access to the sender wich is the CheckBox...)

I've seen another difference with wpf. While trying to remove the GridViewColumnHeader of the list view. It works in Blend but not in Noesis Unity.
I tried :
<GridView.ColumnHeaderContainerStyle>
<Style TargetType="GridViewColumnHeader">
<Setter Property="Visibility" Value="Hidden"/>
</Style>
</GridView.ColumnHeaderContainerStyle>


but also : <Setter Property="Visibility" Value="Collapsed"/>
and even : <Setter Property="Height" Value="0"/>

With no success.

Who is online

Users browsing this forum: ivan.fuertes and 1 guest