Reputation: 10638
I have below listview:
<ListView Margin="10" Name="lvUsers" AlternationCount="2" SelectionMode="Extended">
<ListView.View>
<GridView>
<!-- Checkbox header -->
<GridViewColumn>
<GridViewColumn.Header>
<CheckBox Checked="CheckBox_Checked" Unchecked="CheckBox_Unchecked"/>
</GridViewColumn.Header>
<GridViewColumn.CellTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal">
<CheckBox IsChecked="{Binding IsChecked}" />
</StackPanel>
</DataTemplate>
</GridViewColumn.CellTemplate>
</GridViewColumn>
<GridViewColumn Header="Name" Width="120" DisplayMemberBinding="{Binding Name}" />
<GridViewColumn Header="Age" Width="50" DisplayMemberBinding="{Binding Age}" />
<GridViewColumn Header="Mail" Width="150" DisplayMemberBinding="{Binding Mail}" />
</GridView>
</ListView.View>
<!-- SELECTED ITEM EVENT -->
<ListView.ItemContainerStyle>
<Style TargetType="ListViewItem">
<EventSetter Event="PreviewMouseLeftButtonDown" Handler="ListViewItem_MouseLeftButtonDown" />
</Style>
</ListView.ItemContainerStyle>
</ListView>
and code-behind for the event:
private void ListViewItem_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
{
var item = sender as ListViewItem;
if (item != null && item.IsSelected)
{
//Do your stuff
}
}
and this the data model:
public class User : INotifyPropertyChanged
{
private bool isChecked = false;
private string name = string.Empty;
private int age = 0;
private string mail = string.Empty;
public event PropertyChangedEventHandler PropertyChanged;
private void NotifyPropertyChanged(String propertyName)
{
var handler = PropertyChanged;
if (handler != null)
{
handler(this, new PropertyChangedEventArgs(propertyName));
}
}
public bool IsChecked {
get
{
return this.isChecked;
}
set
{
if (value != this.isChecked)
{
this.isChecked = value;
NotifyPropertyChanged("IsSelected");
}
}
}
public string Name
{
get
{
return this.name;
}
set
{
if (value != this.name)
{
this.name = value;
NotifyPropertyChanged("Name");
}
}
}
public int Age {
get
{
return this.age;
}
set
{
if (value != this.age)
{
this.age = value;
NotifyPropertyChanged("Age");
}
}
}
public string Mail {
get
{
return this.mail;
}
set
{
if (value != this.mail)
{
this.mail = value;
NotifyPropertyChanged("Mail");
}
}
}
}
I have a checkbox at listview header and a checbox for each listview item.
I am trying to detect when a listviewitem is selected and then once selected I want to mark it as checked. Listviewitem event PreviewMouseLeftButtonDown does not work, when fired the item.IsSelected is false, because it is a preview before left mouse button down occurred. There is no MouseClick event, only MouseDoubleClick.
Also, once listviewitem clicked I want to mark as checked (checkbox checked) the item being selected.
How can I do this?
Upvotes: 1
Views: 2249
Reputation: 128060
Bind the IsSelected
property in the ListViewItem Style:
<ListView.ItemContainerStyle>
<Style TargetType="ListViewItem">
<Setter Property="IsSelected" Value="{Binding IsChecked}"/>
</Style>
</ListView.ItemContainerStyle>
As a note, to avoid typographic errors with property names, you might use the CallerMemberName
attribute, which makes the compiler generate the correct property name:
using System.Runtime.CompilerServices;
...
private void NotifyPropertyChanged([CallerMemberName] string propertyName = null)
{
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
}
public bool IsChecked
{
get { return isChecked; }
set
{
isChecked = value;
NotifyPropertyChanged();
}
}
Upvotes: 5