wpf datagrids 绑定和 SelectionMode Single
我在 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
对于这两个全局变量,您需要使用 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.