wpf datagrids 绑定和 SelectionMode Single

发布于 2024-11-16 10:56:53 字数 1703 浏览 5 评论 0原文

我在 wpf 中有两个数据网格:grid1 用于所有可用计划,grid2 用于所选计划。两个网格由两个按钮 (>) 和 (<) 分隔。按钮(>)是将选中的计划添加到关联计划的grid2中,并将其从grid1中删除。按钮(<)用于删除 grid2 的选定项目并将其添加到 grid1 (从选定的计划中删除)。

<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" 也不起作用。我能够选择多行。

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 variables 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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

ㄖ落Θ余辉 2024-11-23 10:56:53

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

问题是列表更改时 UI 没有收到通知。使用 ObservableCollection 会自动通知 UI。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文