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:
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