My DataGrid lists contacts saved in the database:
<DataGrid ItemsSource="{Binding ContactsView}"
Grid.Row="2" Grid.Column="0" Grid.ColumnSpan="2"
x:Name="contactsDataGrid" AutoGenerateColumns="False"
EnableRowVirtualization="True" Padding="10"
CanUserAddRows="False"
RowDetailsVisibilityMode="VisibleWhenSelected"
SelectionChanged="contactsDataGrid_SelectionChanged">
<DataGrid.Columns>
<DataGridCheckBoxColumn Header="Select" Width="50" />
<DataGridTextColumn Binding="{Binding Salutation}" Header="Salut." />
<DataGridTextColumn Binding="{Binding FirstName}" Header="First Name" />
<DataGridTextColumn Binding="{Binding LastName}" Header="Last Name" />
<DataGridTextColumn Binding="{Binding EmailAddress}" Header="Email Address" />
<DataGridTextColumn Binding="{Binding Mobile}" Header="Mobile Number" />
<DataGridTextColumn Binding="{Binding BroadDesignation}" Header="Broad Designation" />
<DataGridTextColumn Binding="{Binding Designation}" Header="Designation" />
</DataGrid.Columns>
</DataGrid>
The user should select a row by clicking on it. When a selection is made, the contact is added to another list which is used later:
private List<Contact> SelectedContacts = new List<Contact>();
private void contactsDataGrid_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
DataGrid ThisGrid = (DataGrid)sender;
Contact SelectedContact = (Contact)ThisGrid.SelectedItem;
if (!SelectedContacts.Contains(SelectedContact))
SelectedContacts.Add(SelectedContact);
}
How can I make it so that when the user makes a selection, the CheckBoxColumn
changes to a state where the checkbox is checked so that the user knows they've already selected that row?
I would add another Property to your class called IsSelected
. In the SelectionChanged
Method you just have to set the property to true and refresh the dataGrid.
Something like the following (not tested):
private void contactsDataGrid_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
DataGrid ThisGrid = (DataGrid)sender;
Contact SelectedContact = (Contact)ThisGrid.SelectedItem;
SelectedContact.IsSelected = true;
contactsDataGrid.Items.Refresh();
if (!SelectedContacts.Contains(SelectedContact))
SelectedContacts.Add(SelectedContact);
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.