I have been looking for two days and I have tried different things but without success
441/5000 I have a datagrid with a checkbox column with a checkbox header.
When I check the header checkbox, I have all my checkboxes checked and the reverse works too.
My problem:
If I check the header checkbox, all my column checkboxes are checked but if I uncheck one my header checkbox does not go off.
Can you help me ? How do I dynamically change the isChecked property of my header checkbox?
Candidate.cs
public class Candidate : INotifyPropertyChanged
{
public int CandidateID { get; set; }
public int RegisterNo { get; set; }
public string CandidateName { get; set; }
private bool _IsSelected = false;
private bool _IsChecked = false;
public bool IsSelected { get { return _IsSelected; } set { _IsSelected = value; OnChanged("IsSelected"); } }
#region INotifyPropertyChanged Members
public event PropertyChangedEventHandler PropertyChanged;
private void OnChanged(string prop)
{
if (PropertyChanged != null)
PropertyChanged(this, new PropertyChangedEventArgs(prop));
}
#endregion
}
MainWindows.xaml.cs
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
List<Candidate> tmp = new List<Candidate>();
tmp.Add(new Candidate()
{
CandidateID = 1,
CandidateName = "One",
RegisterNo = 1
});
tmp.Add(new Candidate()
{
CandidateID = 2,
CandidateName = "Two",
RegisterNo = 2
});
tmp.Add(new Candidate()
{
CandidateID = 3,
CandidateName = "Three",
RegisterNo = 3
});
dgCandidate.ItemsSource = tmp;
}
private void chkSelectAll_Checked(object sender, RoutedEventArgs e)
{
foreach (Candidate c in dgCandidate.ItemsSource)
{
c.IsSelected = true;
}
}
private void chkSelectAll_Unchecked(object sender, RoutedEventArgs e)
{
foreach (Candidate c in dgCandidate.ItemsSource)
{
c.IsSelected = false;
}
}
}
XAML
<DataGrid AutoGenerateColumns="False" CanUserAddRows="False" Height="130" HorizontalAlignment="Left" IsReadOnly="False" Margin="62,164,0,0" Name="dgCandidate" TabIndex="7" VerticalAlignment="Top" Width="466" >
<DataGrid.Columns>
<DataGridTextColumn x:Name="colCandidateID" Binding="{Binding CandidateID}" Header="SlNo" MinWidth="20" IsReadOnly="True" />
<DataGridTextColumn x:Name="colRegistraion" Binding="{Binding RegisterNo}" Header="Reg. No." IsReadOnly="True" />
<DataGridTextColumn x:Name="colCandidate" Binding="{Binding CandidateName}" Header="Name" MinWidth="250" IsReadOnly="True" />
<DataGridCheckBoxColumn Binding="{Binding IsSelected}">
<DataGridCheckBoxColumn.Header>
<CheckBox Name="chkSelectAll" IsChecked="True" Checked="chkSelectAll_Checked" Unchecked="chkSelectAll_Unchecked"></CheckBox>
</DataGridCheckBoxColumn.Header>
</DataGridCheckBoxColumn>
</DataGrid.Columns>
</DataGrid>
I'm curious to see how we can do this?
Sorry for my english and thank you in advance