【问题标题】:WPF's DataGrid is not updating SelectedItems when adding new row添加新行时,WPF DataGrid 未更新 SelectedItems
【发布时间】:2018-03-23 13:14:19
【问题描述】:

在 WPF 中使用 DataGrid 时,当直接从 UI 上的 DataGrid 添加新行时,SelectedItems 属性的大小总是在增长 - 即使在 UI 上总是选择单行进行编辑。

如何克服这个问题?为什么 SelectedItems 属性不能反映 UI 上实际选定项目的数量?

我的代码:

SampleWindow.xaml

<Window x:Class="ALMClient.Controls.SampleWindow"
         xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
         xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
         xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
         xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
         xmlns:local="clr-namespace:ALMClient.Controls"
         mc:Ignorable="d" 
         d:DesignHeight="300" d:DesignWidth="300">
<Grid>
    <Grid.RowDefinitions>
        <RowDefinition Height="0.9*"/>
        <RowDefinition Height="0.1*"/>
    </Grid.RowDefinitions>
    <TabControl Name="tabControl" Grid.Row="0">
        <TabItem Header="Test">
            <DataGrid Name="dataGrid" VerticalContentAlignment="Stretch" HorizontalContentAlignment="Stretch" CanUserAddRows="True" CanUserDeleteRows="True" AutoGenerateColumns="True" 
                      ItemsSource="{Binding GeneralTasks}" SelectionMode="Single" SelectionChanged="dataGrid_SelectionChanged" SelectionUnit="FullRow">

            </DataGrid>
        </TabItem>
    </TabControl>
    <TextBox Name="selectionBox" Grid.Row="1">

    </TextBox>
</Grid>

SampleWindow.xaml.cs

using System.Windows;

namespace ALMClient.Controls
{
    public partial class SampleWindow : Window
    {
        public SampleWindow()
        {
            InitializeComponent();
        }        

        private void dataGrid_SelectionChanged(object sender, System.Windows.Controls.SelectionChangedEventArgs e)
        {
            this.selectionBox.Text = this.dataGrid.SelectedItems.Count.ToString();
        }
    }
}

【问题讨论】:

  • 您的问题不清楚。有多少选定的行,TextBox 显示多少?
  • @mm8 一直有单个项目被选中,但是 SelectedItems.Count 一直在增加 - 2、3、4 等等。

标签: c# wpf datagrid selection rows


【解决方案1】:

您已将选择模式设置为单一

ItemsSource="{Binding GeneralTasks}" **SelectionMode="Single"** 

这意味着所选项目有点学术性。 您应该使用 SelectedItem 属性。

由于您正在绑定 itemssource,因此您没有使用 mvvm 方法并将 selecteditem 绑定到视图模型中的属性似乎很奇怪。 然后,您可以将代码放入该绑定属性的设置器中,并在调用该属性时采取行动,而不是使用 selectionchange 事件。 像这样: https://social.technet.microsoft.com/wiki/contents/articles/30564.wpf-uneventful-mvvm.aspx#Select_From_List_IndexChanged

我用来测试的东西是我有很多不同的风格和东西来演示和探索问题。它充满了垃圾和注释掉的位。

重要的是数据网格和处理程序。 我有

<TabControl Name="tabControl" Grid.Row="0">
    <TabItem Header="Test">
        <DataGrid ItemsSource="{Binding Items}" 
          RowHeight="30"
         x:Name="dg"
         Height="300" 
         SelectionUnit="FullRow"
         AutoGenerateColumns="true"
         Sorting="dg_Sorting"
         HeadersVisibility="All"
         SelectionMode="Extended"
         CanUserDeleteRows="true"
         CanUserAddRows="True"
         AlternationCount="2000"
         AutoGeneratingColumn="dg_AutoGeneratingColumn_1"
         SelectionChanged="dg_SelectionChanged"
           >

我尝试使用单一和扩展的选择模式,绑定到 observablecollection 和数据表的视图。 它总是有效的。

我的处理程序。

private void dg_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
    this.countSelected.Text = dg.SelectedItems.Count.ToString();
}

这是压缩解决方案: https://1drv.ms/u/s!AmPvL3r385QhgooXOPWbZYGwuScJMQ

【讨论】:

  • 对不起,我猜你误会了。我对 SelectedItem 不感兴趣。我想知道的是在 UI 上选择的 SelectedItems 的计数 - 我有例如总是选择单行,但 TextBox 显示不同的数字。
  • 我无法重现您的问题。当您将 selectionmode 设置为 single 时,它​​只能是您在文本框中看到的 1。这就是我得到的。当我将选择模式设置为扩展时,我可以选择更多。我得到了正确的号码。
  • 你能发布你的代码吗?我想检查一下差异。
  • 我将解决方案上传到 onedrive。请记住,它充满了其他东西,但我不知道这将如何影响这一点。事件触发,结果是我所期望的。单身时为 1,扩展时我选择的任何内容的计数。
猜你喜欢
  • 2015-04-06
  • 1970-01-01
  • 2015-12-04
  • 1970-01-01
  • 2018-12-29
  • 1970-01-01
  • 2011-04-16
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多