wpf datagrids binding and SelectionMode Single
I had two datagrids in wpf: grid1 for all available plans and grid2 for the plans been selected. The two grids are separated by two buttons (>) and (<). Button (>) is to add the selected plan to the grid2 of associated plans and remove it from grid1. Button (<) is to remove selected item of grid2 and add it to grid1 (to remove from selected plans).
<DataGrid Name="grdAllPlans" SelectionMode="Single" ItemsSource="{Binding}">
<Button Click="linkClicked">
<Button Click="UnlinkClicked">
<DataGrid Name="grdSelectedPlans" SelectionMode="Single" ItemsSource="{Binding}">
I had two global variabl开发者_Go百科es that have the plans:
static List<PlanDTO> PlansAssociated = new List<PlanDTO>(); //contain plans selected
static List<PlanDTO> PlansAvailable = new List<PlanDTO>(); //contain all plans not selected
This is the method call to associate a plan (remove from grid1 and add it to grid2):
private void linkClicked(object sender, RoutedEventArgs e)
{
if (grdAllPlans.SelectedItem != null)
{
PlanDTO selectedPlan = (PlanDTO)grdAllPlans.SelectedItem;
PlansAvailable.Remove(selectedPlan); //remove from collection PlansAvailable
PlansAssociated.Add(selectedPlan); //add it to selected collection
//Update grid1
srcCollectionViewAvailable.Source = PlansAvailable;
grdPlansDisponibles.ItemsSource = srcCollectionViewAvailable.View;
//Update grid2
srcCollectionViewAssociated.Source = PlansAssociated;
grdPlansAsociés.ItemsSource = srcCollectionViewAssociated.View;
grdPlansAsociés.UnselectAll();
grdPlansDisponibles.UnselectAll();
}
}
The problem it is not working. The first time that I add a plan to the selected plan grid it do it well, but after that both grid do not get refresh. Also SelectionMode="Single" do not work. I am been able to select multiple rows.
You need to use ObservableCollection, instead of List for those 2 global variables.
The problem is that the UI wasn't notified when list changes. Using ObservableCollection would notify the UI automatically.
精彩评论