2009-04-30 81 views
17

我期待在C#winforms应用程序中创建可编辑的ListView,其中用户可以双击单元格以更改其内容。如果有人能够提供一些指导和/或示例,那将是很棒的。我不打算使用任何商业产品。可编辑列表查看

+0

你的问题可能在这里得到解答:[C#:如何编辑在ListView项目和子项目?] [1] [1]:http://stackoverflow.com/questions/471859/ c-how-do-you-edit-items-and-subitems-in-a-listview – 2012-07-10 11:08:40

+0

我不同意我们不应该看像DataGridView,ObjectListView这样的ListView替代品,或者甚至像[this]这样的商业解决方案(http:// 10tec.com/articles/editable-listview-replacement.aspx),这是足够便宜的。使用文本框编辑器在ListView项目/子文件中实现众所周知的方法有许多缺点,您必须自己解决。例如,您需要提供一个良好的键盘界面来编辑子项目,文本框应该与ListView一起滚动,等等。使用良好的第三方解决方案可以节省数小时的编码,最终您甚至可以赚取更多。 – TecMan 2016-07-28 11:48:57

回答

14

你在问错误的问题:)

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

+4

这是一个意见问题。 DataGridView并不总是适合使用场景或提供所需的外观。但是,这是一个可能的选择,取决于OP的目标。 – 2009-04-30 19:33:42

+7

您无法使用DataGridView执行分组视图。再加上一系列只有ListView支持的其他功能。 – code4life 2011-04-08 22:22:39

6

一个ObjectListView会做正是等等。它是一个正常的.NET ListView的包装。它是开源的。

它的网站上有一个Getting Started帮助你开始,以及一整页专门cell editing

+1

感谢您的回复。这个自定义控件看起来很有趣,但对于我正在尝试做的事情可能会过度。尽管如此,我仍会试验一下。 – 2009-05-13 15:41:38

0

的DataGridView是你的朋友 SourceGrid是替代

1

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

-1

您可以使用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> 

尼克Hanshaw

-1

我最近遇到了这个问题。从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

0

是的,使用DataGridView。

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