Hi All,
I have a listview and I am binding this ListView using an XML file. The first column of the ListView is CheckBox. I am using Two converters to set the checkbox properties isChecked and isEnabled to set the values. I have a checkbox in the header too corresponding to my first column (Checkbox column). On Click of my CheckBox in the header, I need to check all enabled checkboxes in the ListView as well as on unckeck of header checkbox should be unchecked all enabled checkboxes in the ListView. Please find below code snippet
XAML Part,
<
Window x:Class="ListViewWithControls.ListViewWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:ValueConverters"
Title="Window1" Height="306" Width="450">
<Window.Resources>
<local:ConvertStringToBool x:Key="boolConv"/>
<local:CheckBoxEnableDisable x:Key="enableConv"/>
</Window.Resources>
<Grid>
<ListView Name="lvPlace" ItemsSource="{Binding Source={StaticResource PlaceList}, XPath=Rows/Row}" FontFamily="Calibri" FontSize="14" Foreground="Black" >
<ListView.View>
<GridView>
<GridViewColumn Width="85">
<GridViewColumn.HeaderTemplate>
<DataTemplate>
<CheckBox Name="chkStatusHeader" Checked="selectAll" Unchecked="unSelectAll"></CheckBox>
</DataTemplate>
</GridViewColumn.HeaderTemplate>
<GridViewColumn.CellTemplate>
<DataTemplate>
<CheckBox Name="chkStatus" IsChecked="{Binding XPath=Cell1,Converter={StaticResource boolConv},Mode=OneWay}" IsEnabled="{Binding XPath=Cell1,Converter={StaticResource enableConv},Mode=OneWay}"></CheckBox>
</DataTemplate>
</GridViewColumn.CellTemplate>
</GridViewColumn>
<GridViewColumn>
<GridViewColumn.Header>
<TextBlock Text="Place" Style="{StaticResource ColHeaderText}" />
</GridViewColumn.Header>
<GridViewColumn.CellTemplate>
<DataTemplate>
<TextBlock Name="tnLinkText" Text="{Binding XPath=Cell2}" />
</DataTemplate>
</GridViewColumn.CellTemplate>
</GridViewColumn>
</GridView>
</ListView.View>
</ListView>
</Grid>
</
Window>
Please find below my converters
namespace
ValueConverters
{
[
ValueConversion(typeof(string), typeof(bool))]
class ConvertStringToBool:IValueConverter
{
#region
IValueConverter Members
public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
string data = value.ToString();
if (data == "Approved")
{
return true;
}
return false;
}
public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
//throw new NotImplementedException();
return true;
}
#endregion
}
[
ValueConversion(typeof(string), typeof(bool))]
class CheckBoxEnableDisable : IValueConverter
{
#region
IValueConverter Members
public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
string data = value.ToString();
if (data == "Registered")
{
return true;
}
return false;
}
public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
//throw new NotImplementedException();
return true;
}
#endregion
}
}
The following methods I have written in the code behind to handle check/Uncheck all check boxes....
private void selectAll(object sender, RoutedEventArgs e)
{
for (int i = 0; i < Place.Items.Count; i++)
{
}
}
private void unSelectAll(object sender, RoutedEventArgs e)
{
for (int i = 0; i < Place.Items.Count; i++)
{
}
}
Could you plz someone help me out how can solve my issue.
I would definitely appreciate you..
Thanks & Regards,
Faisal. N