Hi all,
I have an application that has many Checkboxes (> 100). I also have a datatable that has 2 columns (1 of type string and 1 of type bool). Is there any simply way that I enable two way binding between each Checkbox and the datatable?
Specifically what I'm trying to do is for a checkbox, I'm try to use IsChecked="{Binding Path= XXX, Mode=TwoWay}" where XXX represents the cell of the datatable. How I identify the cell is by the fact that
1) The column is always column 1, i.e. the Boolean column
2) The row is determined by the Name of the checkbox using the binding. For every checkbox, I have an entry in column 0 of the datatable with that Checkbox's name. Therefore the idea is that based on the Checkbox name I should be able to identify the row of the datatable and update the bool column of that row with the state of the checkbox, true or false.
My biggest problem is that this binding needs to be two way so any updates to my datatable within my code would also update the tick status of a checkbox. If I only needed oneway, I could do this easily enough by defining methods for Checked and Unchecked and using the sender Name within those methods to identify the row of the datatable.
All feedback appreciated. Please keep it simple cause I ain't no expert!
Cheers
Shav
parthiban arthanariPosted Aug 8, 2014, 1:06 AM
Hi Shane,
You can use Converter in binding. Here I used my own BooleanToStringConverter, which will convert Boolean value to string value.
public class BooleanToStringConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
if ((bool) value)
return "True";
else
return "False";
}
public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
throw new NotImplementedException();
}
}
Now I have datatable with two columns namely Name(stirng) and Value(boolean). Then I bind the datatable to DataGrid, which will generate rows automatically with the bounded datatable values.
MyDataGrid.DataContext = table.DefaultView;
XAML:
<Grid>
<Grid.Resources>
<local:BooleanToStringConverter x:Key="BooleanToStringConverter">local:BooleanToStringConverter>
Grid.Resources>
<DataGrid x:Name="MyDataGrid" ItemsSource="{Binding}" CanUserAddRows="False" AutoGenerateColumns="False" Width="200" HorizontalAlignment="Left">
<DataGrid.Columns>
<DataGridCheckBoxColumn Header="CheckBox" Binding="{Binding Value, Mode=TwoWay}">DataGridCheckBoxColumn>
<DataGridTextColumn Header="Name" Binding="{Binding Name}">DataGridTextColumn>
<DataGridTextColumn Header="Value" Binding="{Binding Value, Converter={StaticResource BooleanToStringConverter}}" Width="*">DataGridTextColumn>
DataGrid.Columns>
DataGrid>
Grid>
For each datarow in datatable, I have created three columns in my data grid(Checkbox, Text, Text). You can notice that I bind Boolean value to both the checkbox column and text column. So when you check or un-check the checkbox, it will automatically change Value column in data grid via binding. And also converter plays vital role, it accepts Boolean value from data table and convert it to string value. I used converter in Value column of my data grid.
You can find my sample below.
Kindly mark as answer, if this helps you.
Thanks,
Parthiban