C# 可编辑列表视图

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/808588/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-05 02:03:46  来源:igfitidea点击:

Editable ListView

c#winformsuser-interfacelistview

提问by THE DOCTOR

I am looking to create an editable ListView in a C# winforms application where a user may double click on a cell in order to change its contents. It would be great if someone could provide my with some guidance and/or an example. I am not looking to use any commercial products.

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

回答by Simon Gillbee

You're asking the wrong question :)

你问错了问题:)

A ListView is not the correct control. Use the DataGridViewcontrol. It can be configured to look just like a ListView, but it supports in-place editing of cells.

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

回答by jblaske

From the sounds of it, you might want to consider using the DataGridView instead.

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

DataGridView (MSDN)

数据网格视图 (MSDN)

回答by Iralda Mitro

回答by Grammarian

An ObjectListViewwill do exactly that and much more. It is a wrapper around a normal .NET ListView. It is open source.

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

Its website has a Getting Startedto help you begin, as well as a whole page devoted to cell editing

它的网站有一个入门指南来帮助你开始,还有一个专门介绍单元格编辑的页面

回答by Pavels

DataGridView is your friend SourceGrid is alternative

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

回答by A. Abiri

You could use the listview's DoubleClick event, and when it is called, you would open a new form where the user would enter a new value for the selected item. Then when the user has pressed ok, you would edit the value of the specific item to what the user has entered.

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

回答by Nick Hanshaw

You could use a DataTemplate to specify that the column contains a textbox (if editable) or a textblock (if not editable) then bind the textbox to the class property from your Source Object Collection that is bound to your listview's itemsource.

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

Nick Hanshaw

尼克汉肖

回答by Derek W

I recently encountered this issue. After taking the hint from Simon Gillbee that it is possible to configure DataGridView to appear much like ListView, I searched for a sensible solution to accomplish just that. The following code worked well for me. Source here.

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

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);
        }
    }
}

Just inherit from DataGridViewand override the OnPaintmethod.

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

You can change the various properties of the control to suit your needs and preferences.

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

For those who need assistance with incorporating a custom control into their project look here.

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

回答by dcarl661

Yes, use a DataGridView.

是的,使用 DataGridView。

Not only can you edit a cell, but if you declare a generic List where T is the class you want to display in the grid, you can set the DataSource=that list and as you edit the gridview you are actually editing the List automagically!

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