I have the following ListView:
<ListView Name="listView">
<ListView.View>
<GridView>
<GridView.ColumnHeaderContainerStyle>
<Style TargetType="{x:Type GridViewColumnHeader}">
<Setter Property="Visibility"
Value="Collapsed"/>
</Style>
</GridView.ColumnHeaderContainerStyle>
<GridViewColumn>
<GridViewColumn.CellTemplate>
<DataTemplate>
<StackPanel>
<CheckBox
Margin="0"
VerticalAlignment="Center"
IsChecked="{Binding IsChecked}"
Visibility="{Binding IsChecked, Converter={StaticResource boolToVis}}">
</CheckBox>
</StackPanel>
</DataTemplate>
</GridViewColumn.CellTemplate>
</GridViewColumn>
<GridViewColumn>
<GridViewColumn.CellTemplate>
<DataTemplate>
<TextBlock Margin="0"
Text="{Binding Text}"/>
</DataTemplate>
</GridViewColumn.CellTemplate>
</GridViewColumn>
</GridView>
</ListView.View>
</ListView>
The items in the ListView are of the below type:
public class CheckBoxListViewItemSource : INotifyPropertyChanged
{
public CheckBoxListViewItemSource(String text)
{
m_text = text;
}
public bool IsChecked
{
get { return m_checked; }
set
{
if (m_checked == value) return;
m_checked = value;
RaisePropertyChanged("IsChecked");
}
}
public String Text
{
get { return m_text; }
set
{
if (m_text == value) return;
m_text = value;
RaisePropertyChanged("Text");
}
}
public override string ToString()
{
return Text;
}
public event PropertyChangedEventHandler PropertyChanged;
private void RaisePropertyChanged(string propName)
{
PropertyChangedEventHandler eh = PropertyChanged;
if (eh != null)
{
eh(this, new PropertyChangedEventArgs(propName));
}
}
private bool m_checked;
private String m_text;
}
The visibility of the checkbox in the ListView is bound to the value of IsChecked of the ListViewItem. The converter is a simple bool to visibility converter:
public class BoolToVisibilityConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter,
CultureInfo culture)
{
if (value is Boolean)
{
return ((bool)value) ? Visibility.Visible : Visibility.Collapsed;
}
return value;
}
public object ConvertBack(object value, Type targetType, object parameter,
CultureInfo culture)
{
throw new NotImplementedException();
}
}
In the code behind of the ListView I have:
void listView_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
foreach (var item in e.RemovedItems)
{
CheckBoxListViewItemSource source = item as CheckBoxListViewItemSource;
source.IsChecked = false;
}
foreach (var item in e.AddedItems)
{
CheckBoxListViewItemSource source = item as CheckBoxListViewItemSource;
source.IsChecked = true;
}
}
The binding for the checkbox visibility is not working for me. The default IsChecked value is false so the list appears with no checkboxes. If I select an item, the checkbox does not appear.
However, if I set the default value of IsChecked to true, all of the list items appear with a checkbox and if I select an item and then deselect it, the checkbox correctly disappears.
What I am trying to achieve is that all of the items start off with no checkbox, selecting an item displays a checked checkbox, and deselecting an item hides the checkbox.
Any ideas where I am going wrong?