【问题标题】:Editable ListView可编辑的列表视图
【发布时间】:2010-10-22 22:56:47
【问题描述】:

我希望在 C# winforms 应用程序中创建一个可编辑的 ListView,用户可以在其中双击单元格以更改其内容。如果有人可以为我提供一些指导和/或示例,那就太好了。我不打算使用任何商业产品。

【问题讨论】:

  • 您的问题可能在这里得到解答:[C#: How do you edit items and subitems in a listview?][1] [1]: stackoverflow.com/questions/471859/…
  • 我不同意我们不应该考虑像 DataGridView、ObjectListView 这样的 ListView 替代方案,甚至是像 this 这样的商业解决方案,它们已经足够便宜了。在 ListView 项目/子项上使用文本框编辑器实现众所周知的方法有许多缺点,您必须自己解决。例如,您需要提供良好的键盘界面来编辑子项,文本框应与 ListView 一起滚动等。使用良好的 3rd-party 解决方案可能会节省数小时的编码,最终您甚至可以获得更多。

标签: c# winforms user-interface listview


【解决方案1】:

你问错问题了:)

ListView 不是正确的控件。使用DataGridView 控件。它可以配置为看起来像 ListView,但它支持对单元格的就地编辑。

【讨论】:

  • 见仁见智。 DataGridView 并不总是适合使用场景或提供所需的外观。但是,它是一种可能的替代方案,具体取决于 OP 的目标。
  • 您不能使用 DataGridView 进行分组视图。加上许多只有 ListView 支持的其他功能。
  • 花了我一分钟,但是,嗯,是的......“列表VIEW”......查看 - 不 - 编辑。有趣的是,DataGridVIEW 允许一个 EDIT。嗯。
【解决方案2】:

ObjectListView 可以做到这一点,甚至更多。它是一个普通的 .NET ListView 的包装器。它是开源的。

它的网站有一个Getting Started 来帮助你开始,还有一个专门用于cell editing 的页面

【讨论】:

  • 感谢您的回复。这个自定义控件看起来很有趣,但对于我正在尝试做的事情来说可能有点矫枉过正。尽管如此,我还是会尝试一下。
  • 其实它并不是真正的自定义控件。它只是一个简单的 ListView 的有用包装器。但我们不会告诉任何人:)
  • 它也可能不是所有项目的合适许可证(因为它似乎在 GPLv3 下)
【解决方案3】:

从它的声音来看,您可能需要考虑改用 DataGridView。

DataGridView (MSDN)

【讨论】:

    【解决方案4】:

    您可以使用列表视图的 DoubleClick 事件,当它被调用时,您将打开一个新表单,用户可以在其中为所选项目输入新值。然后当用户按下 ok 时,您可以将特定项目的值编辑为用户输入的内容。

    【讨论】:

      【解决方案5】:

      DataGridView 是你的朋友 SourceGrid 是另一种选择

      【讨论】:

        【解决方案6】:

        您可以使用 DataTemplate 指定列包含文本框(如果可编辑)或文本块(如果不可编辑),然后将文本框绑定到绑定到列表视图项目源的源对象集合中的类属性。

        <Window.Resources>
            <ResourceDictionary>
                <DataTemplate x:Key="NameHeader">
                    <StackPanel Orientation="Horizontal">
                        <TextBlock Text="Name" VerticalAlignment="Center" Margin="10,0,0,0" />
                    </StackPanel>
                </DataTemplate>
                <DataTemplate x:Key="NameCell">
                    <StackPanel Orientation="Horizontal">
                        <TextBox Text="{Binding Path=Name}" VerticalAlignment="Center" Margin="10,0,0,0" />
                    </StackPanel>
                </DataTemplate>
            </ResourceDictionary>
        </Window.Resources>
        
        <Grid>
            <ListView x:Name="lvwList" Height="200" VerticalAlignment="Top" ItemsSource="{Binding Path=SourceObjectCollection}">
                <ListView.View>
                    <GridView>
                        <GridViewColumn Header="Name" HeaderTemplate="{StaticResource NameHeader}" CellTemplate="{StaticResource NameCell}" Width="140" />
                    </GridView>
                </ListView.View>
            </ListView>
        </Grid>
        

        尼克·汉肖

        【讨论】:

        • 欢迎来到 SO,尼克。这是一个很好的答案,但不幸的是,这个问题被标记为winforms,而不是wpf
        【解决方案7】:

        是的,使用 DataGridView。

        你不仅可以编辑一个单元格,而且如果你声明一个通用列表,其中 T 是你想在网格中显示的类,你可以设置 DataSource=那个列表,当你编辑 gridview 时,你实际上是在编辑自动列出!

        【讨论】:

          【解决方案8】:

          【讨论】:

          • 这是一篇 WPF 文章。这是想要的,还是我们在这里直接看 WinForms?标签表示 WinForms。
          【解决方案9】:

          我最近遇到了这个问题。在得到 Simon Gillbee 的提示后,可以将 DataGridView 配置为看起来很像 ListView,我搜索了一个明智的解决方案来实现这一点。以下代码对我来说效果很好。 Source here.

          class GridLineDataGridView : DataGridView
          {
              public GridLineDataGridView()
              {
                  this.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.Fill;
              }
          
              protected override void OnPaint(PaintEventArgs e)
              {
                  base.OnPaint(e);
          
                  int rowHeight = this.RowTemplate.Height;
          
                  int h = this.ColumnHeadersHeight + rowHeight * this.RowCount;
                  int imgWidth = this.Width - 2;
                  Rectangle rFrame = new Rectangle(0, 0, imgWidth, rowHeight);
                  Rectangle rFill = new Rectangle(1, 1, imgWidth - 2, rowHeight);
                  Rectangle rowHeader = new Rectangle(2, 2, this.RowHeadersWidth - 3, rowHeight);
          
                  Pen pen = new Pen(this.GridColor, 1);
          
                  Bitmap rowImg = new Bitmap(imgWidth, rowHeight);
                  Graphics g = Graphics.FromImage(rowImg);
                  g.DrawRectangle(pen, rFrame);
                  g.FillRectangle(new SolidBrush(this.DefaultCellStyle.BackColor), rFill);
                  g.FillRectangle(new SolidBrush
                     (this.RowHeadersDefaultCellStyle.BackColor), rowHeader);
          
                  int w = this.RowHeadersWidth - 1;
                  for (int j = 0; j < this.ColumnCount; j++)
                  {
                      g.DrawLine(pen, new Point(w, 0), new Point(w, rowHeight));
                      w += this.Columns[j].Width;
                  }
          
                  int loop = (this.Height - h) / rowHeight;
                  for (int j = 0; j < loop + 1; j++)
                  {
                      e.Graphics.DrawImage(rowImg, 1, h + j * rowHeight);
                  }
              }
          }
          

          只需从DataGridView 继承并覆盖OnPaint 方法。

          您可以更改控件的各种属性以满足您的需要和偏好。

          对于那些在将自定义控件合并到他们的项目中需要帮助的人,请查看 here

          【讨论】:

          • 示例不完整。对于 WPF 新手,您将新的 DataGrid 放在哪里?
          • @CJohnson:这个问题是关于 WinForms 和 not WPF。请检查问题标签和上下文。
          猜你喜欢
          • 1970-01-01
          • 2011-08-04
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2018-06-15
          相关资源
          最近更新 更多