How to prevent the double-click/single-click event from checking checkbox on a listview

BobG 41 Reputation points
2022-05-27T08:53:45.04+00:00

Hi,

I'm new with WPF (but not with C#) and I'm trying to use a Listview with checkboxes.
I successfully created a ListView with checkboxes on the first column but I would like the checkbox being checked only if I click on the column where the checkbox is and keep its state even if I click on a different row.
Normally if I click somewhere on a row, the row gets selected and the checkbox appears checked, if I select another row the previous line gets unselected and the previous checkbox gets unchecked.
If I double-click on a row (to let the user edit the row for example) the checkbox gets checked.
Is there a way to prevent this behaviour?
Thank you, regards.

Roberto

Developer technologies Windows Presentation Foundation
{count} votes

Accepted answer
  1. Hui Liu-MSFT 48,676 Reputation points Microsoft External Staff
    2022-05-27T09:35:41.993+00:00

    If your CheckBox is bound to a property of an item (class), the CheckBox will not tick when the item is selected.

    MainWindow.xaml:

    <Grid>  
            <StackPanel >  
                <ListView Width="300" Height="300" ItemsSource="{Binding Datas}"    >  
                    <ListView.View >  
                        <GridView AllowsColumnReorder="False">  
                            <GridViewColumn Header="Name" Width="120">  
                                <GridViewColumn.CellTemplate>  
                                    <DataTemplate>  
                                        <TextBlock Text="{Binding Name}" TextWrapping="Wrap"/>  
                                    </DataTemplate>  
                                </GridViewColumn.CellTemplate>  
                            </GridViewColumn>  
                            <GridViewColumn Header="Flag" Width="120">  
                                <GridViewColumn.CellTemplate>  
                                    <DataTemplate>  
                                        <CheckBox   IsChecked="{Binding Path=Flag, Mode=TwoWay}"    Content="{Binding Path=Content,Mode=TwoWay}"/>  
                                    </DataTemplate>  
                                </GridViewColumn.CellTemplate>  
                            </GridViewColumn>  
                        </GridView>  
                    </ListView.View>  
                </ListView>  
            </StackPanel>  
        </Grid>  
    

    MainWindow.xaml.cs:

    using System.Collections.ObjectModel;  
    using System.ComponentModel;  
    using System.Runtime.CompilerServices;  
    using System.Windows;  
    
    namespace PreventCheckBoxInTheListView  
    {  
      public partial class MainWindow : Window  
      {  
        ViewModel vm = new ViewModel();  
        public MainWindow()  
        {  
          InitializeComponent();  
          DataContext = vm;  
        }  
      }  
      public class ViewModel : INotifyPropertyChanged  
      {  
        ObservableCollection<Item> datas = new ObservableCollection<Item>();  
        public ObservableCollection<Item> Datas  
        {  
          get  
          {  
            return datas;  
          }  
          set  
          {  
            datas = value;  
            OnPropertyChanged("Datas");  
          }  
        }  
        public ViewModel()  
        {  
          datas.Add(new Item("name1", "content1", true));  
          datas.Add(new Item("name2", "content2", false));  
          datas.Add(new Item("name3", "content3", true));  
          datas.Add(new Item("name4", "content4", false));  
        }  
        public event PropertyChangedEventHandler PropertyChanged;  
        protected void OnPropertyChanged([CallerMemberName] string name = null)  
        {  
          PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(name));  
        }  
      }  
      public class Item  
      {  
        public string Name { get; set; }  
        public string Content { get; set; }  
        public bool Flag { get; set; }  
        public Item()  
        {  
        }  
        public Item(string name, string content,bool flag)  
        {  
          Name = name;  
          Flag = flag;  
          Content= content;  
        }  
      }  
    
    }  
    

    The result:
    206138-11.gif


    If the response is helpful, please click "Accept Answer" and upvote it.
     Note: Please follow the steps in our [documentation][5] to enable e-mail notifications if you want to receive the related email notification for this thread. 

    [5]: https://learn.microsoft.com/en-us/answers/articles/67444/email-notifications.html

    0 comments No comments

1 additional answer

Sort by: Most helpful
  1. BobG 41 Reputation points
    2022-05-27T10:16:06.08+00:00

    Thank you, that worked !
    Regards,

    Roberto

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.