xaml
<
Window
x:Class
="TestCheckListView.Window1"
xmlns ="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x ="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:l ="clr-namespace:TestCheckListView"
Title ="Window1" Height ="398" Width ="400" >
< Window.Resources >
< l:CheckBoxVisibilityConverter x:Key ="CheckBoxVisibilityConverter" />
< GridView x:Key ="gridview" >
< GridViewColumn Header ="Name" >
< GridViewColumn.CellTemplate >
< DataTemplate >
< StackPanel Orientation ="Horizontal" >
< CheckBox IsChecked =" {Binding Path=IsSelected, RelativeSource={RelativeSource AncestorType={x:Type ListViewItem}}} " >
< CheckBox.Visibility >
< MultiBinding Converter =" {StaticResource CheckBoxVisibilityConverter} " >
< Binding Path ="IsMouseOver" RelativeSource =" {RelativeSource AncestorType={x:Type ListViewItem}} " />
< Binding Path ="IsSelected" RelativeSource =" {RelativeSource AncestorType={x:Type ListViewItem}} " />
</ MultiBinding >
</ CheckBox.Visibility >
</ CheckBox >
< TextBlock Text =" {Binding Name} " />
</ StackPanel >
</ DataTemplate >
</ GridViewColumn.CellTemplate >
</ GridViewColumn >
< GridViewColumn Header ="Age" DisplayMemberBinding =" {Binding Age} " />
< GridViewColumn Header ="Sex" DisplayMemberBinding =" {Binding Sex} " />
</ GridView >
</ Window.Resources >
< Grid Margin ="10" >
< ListView Name ="lv" View =" {StaticResource gridview} " Margin ="0,0,0,75" />
< Button Height ="23" HorizontalAlignment ="Left" Margin ="90,0,0,28" Name ="button1" VerticalAlignment ="Bottom" Width ="75" Click ="button1_Click" > Button </ Button >
</ Grid >
</ Window >
xmlns ="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x ="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:l ="clr-namespace:TestCheckListView"
Title ="Window1" Height ="398" Width ="400" >
< Window.Resources >
< l:CheckBoxVisibilityConverter x:Key ="CheckBoxVisibilityConverter" />
< GridView x:Key ="gridview" >
< GridViewColumn Header ="Name" >
< GridViewColumn.CellTemplate >
< DataTemplate >
< StackPanel Orientation ="Horizontal" >
< CheckBox IsChecked =" {Binding Path=IsSelected, RelativeSource={RelativeSource AncestorType={x:Type ListViewItem}}} " >
< CheckBox.Visibility >
< MultiBinding Converter =" {StaticResource CheckBoxVisibilityConverter} " >
< Binding Path ="IsMouseOver" RelativeSource =" {RelativeSource AncestorType={x:Type ListViewItem}} " />
< Binding Path ="IsSelected" RelativeSource =" {RelativeSource AncestorType={x:Type ListViewItem}} " />
</ MultiBinding >
</ CheckBox.Visibility >
</ CheckBox >
< TextBlock Text =" {Binding Name} " />
</ StackPanel >
</ DataTemplate >
</ GridViewColumn.CellTemplate >
</ GridViewColumn >
< GridViewColumn Header ="Age" DisplayMemberBinding =" {Binding Age} " />
< GridViewColumn Header ="Sex" DisplayMemberBinding =" {Binding Sex} " />
</ GridView >
</ Window.Resources >
< Grid Margin ="10" >
< ListView Name ="lv" View =" {StaticResource gridview} " Margin ="0,0,0,75" />
< Button Height ="23" HorizontalAlignment ="Left" Margin ="90,0,0,28" Name ="button1" VerticalAlignment ="Bottom" Width ="75" Click ="button1_Click" > Button </ Button >
</ Grid >
</ Window >
c#
using
System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Collections.ObjectModel;
namespace TestCheckListView
{
/// <summary>
/// Interaction logic for Window1.xaml
/// </summary>
public partial class Window1 : Window
{
public Window1()
{
InitializeComponent();
this .lv.ItemsSource = this .persons;
this .persons.Add( new Person( " John " , 12 , true ));
this .persons.Add( new Person( " Rose " , 21 , false ));
this .persons.Add( new Person( " Jack " , 12 , true ));
this .persons.Add( new Person( " Jenny " , 21 , false ));
this .persons.Add( new Person( " Tom " , 12 , true ));
this .persons.Add( new Person( " Alma " , 21 , false ));
}
private ObservableCollection < Person > persons = new ObservableCollection < Person > ();
private void button1_Click( object sender, RoutedEventArgs e)
{
foreach (Person item in lv.SelectedItems)
{
MessageBox.Show(item.Name);
}
}
}
public class Person
{
public Person(String name, Int32 age, Boolean bMale)
{
this .Name = name;
this .Age = age;
this .Sex = bMale ? " M " : " F " ;
}
public String Name { get ; set ; }
public Int32 Age { get ; set ; }
public String Sex { get ; set ; }
}
public class CheckBoxVisibilityConverter : IMultiValueConverter
{
public object Convert( object [] values, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
try
{
Boolean isMouseOver = (Boolean)(values[ 0 ]);
Boolean isSelected = (Boolean)(values[ 1 ]);
if (isSelected)
return Visibility.Visible;
else if (isMouseOver)
return Visibility.Visible;
else
return Visibility.Hidden;
}
catch { return Visibility.Hidden; }
}
public object [] ConvertBack( object value, Type[] targetTypes, object parameter, System.Globalization.CultureInfo culture)
{
return null ;
}
}
}
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Collections.ObjectModel;
namespace TestCheckListView
{
/// <summary>
/// Interaction logic for Window1.xaml
/// </summary>
public partial class Window1 : Window
{
public Window1()
{
InitializeComponent();
this .lv.ItemsSource = this .persons;
this .persons.Add( new Person( " John " , 12 , true ));
this .persons.Add( new Person( " Rose " , 21 , false ));
this .persons.Add( new Person( " Jack " , 12 , true ));
this .persons.Add( new Person( " Jenny " , 21 , false ));
this .persons.Add( new Person( " Tom " , 12 , true ));
this .persons.Add( new Person( " Alma " , 21 , false ));
}
private ObservableCollection < Person > persons = new ObservableCollection < Person > ();
private void button1_Click( object sender, RoutedEventArgs e)
{
foreach (Person item in lv.SelectedItems)
{
MessageBox.Show(item.Name);
}
}
}
public class Person
{
public Person(String name, Int32 age, Boolean bMale)
{
this .Name = name;
this .Age = age;
this .Sex = bMale ? " M " : " F " ;
}
public String Name { get ; set ; }
public Int32 Age { get ; set ; }
public String Sex { get ; set ; }
}
public class CheckBoxVisibilityConverter : IMultiValueConverter
{
public object Convert( object [] values, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
try
{
Boolean isMouseOver = (Boolean)(values[ 0 ]);
Boolean isSelected = (Boolean)(values[ 1 ]);
if (isSelected)
return Visibility.Visible;
else if (isMouseOver)
return Visibility.Visible;
else
return Visibility.Hidden;
}
catch { return Visibility.Hidden; }
}
public object [] ConvertBack( object value, Type[] targetTypes, object parameter, System.Globalization.CultureInfo culture)
{
return null ;
}
}
}