【问题标题】:wpf datagrids binding and SelectionMode Singlewpf datagrids 绑定和 SelectionMode Single
【发布时间】:2011-06-22 15:52:12
【问题描述】:

我在 wpf 中有两个数据网格:grid1 用于所有可用计划,grid2 用于选择的计划。这两个网格由两个按钮 (>) 和 () 是将选中的计划添加到关联计划的网格 2 中,并从网格 1 中删除。按钮 (

<DataGrid  Name="grdAllPlans" SelectionMode="Single" ItemsSource="{Binding}">
<Button Click="linkClicked">
<Button Click="UnlinkClicked">
<DataGrid  Name="grdSelectedPlans" SelectionMode="Single" ItemsSource="{Binding}">

我有两个有计划的全局变量:

static List<PlanDTO> PlansAssociated = new List<PlanDTO>();  //contain plans selected
static List<PlanDTO> PlansAvailable = new List<PlanDTO>();   //contain all plans not selected

这是关联计划的方法调用(从grid1中删除并添加到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();
            }

    }

它不起作用的问题。我第一次将计划添加到选定的计划网格时它做得很好,但之后两个网格都没有刷新。 SelectionMode="Single" 也不起作用。我可以选择多行。

【问题讨论】:

    标签: wpf binding grid


    【解决方案1】:

    对于这两个全局变量,您需要使用 ObservableCollection,而不是 List。

    问题是列表更改时未通知 UI。使用 ObservableCollection 会自动通知 UI。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-08-27
      • 2016-06-01
      • 2012-03-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多