简体   繁体   English

可编辑的 WPF GridView 行

[英]Editable WPF GridView Row

I am trying to create a databound WPF GridView whose rows can either be read-only or editable (by double-clicking or through a context menu).我正在尝试创建一个数据绑定的 WPF GridView,其行可以是只读的或可编辑的(通过双击或通过上下文菜单)。 I would like for the row to return to a read-only state if any of its editable controls loses focus.如果其任何可编辑控件失去焦点,我希望该行返回只读状态。 The functionality I am looking for is very similar to this example but with an entire row being editted simultaneously (rather than a single cell).我正在寻找的功能与此示例非常相似,但同时编辑了整行(而不是单个单元格)。 Does anyone know how to implement this?有谁知道如何实现这个?

在codeplex处也有来自Microsoft的“官方” wpf datagrid: http//www.codeplex.com/wpf

With the ListView + GridView control il quite complex because this control "thinks in column" so you have to create a template for every column and switch the read-only template with edit template (for every cell). 使用ListView + GridView控件非常复杂,因为该控件“在列中认为”,因此您必须为每列创建一个模板,并用编辑模板(对于每个单元格)切换只读模板。 I suggest you to take a look a the xceed DataGrid. 我建议您看一下xceed DataGrid。 It's free and it implements the edit functionality in a simpler way (you can find info here: http://xceed.com/Grid_WPF_Intro.html ) 它是免费的,并且以更简单的方式实现了编辑功能(您可以在此处找到信息: http : //xceed.com/Grid_WPF_Intro.html

The latest WPF has its own DataGrid. 最新的WPF具有自己的DataGrid。 http://wpftutorial.net/DataGrid.html http://wpftutorial.net/DataGrid.html

This is an old question with an old answer, that this answer no longer works!这是一个带有旧答案的老问题,这个答案不再有效! In .Net 6, for editing Grid-View in WPF, you must use the "CurrentCellChanged" event.在 .Net 6 中,要在 WPF 中编辑 Grid-View,您必须使用“CurrentCellChanged”事件。

UI:用户界面:

<DataGrid x:Name="dataGrid" Margin="0,50,0,0" CurrentCellChanged="dataGrid_CurrentCellChanged"/>

Code:代码:

public MainWindow()
{
    InitializeComponent();

    List<myDataType>? tmp;
    //Add data
    dataGrid.ItemsSource = tmp;
}

private void dataGrid_CurrentCellChanged(object sender, EventArgs e)
{
    myDataType tmp = (sender as DataGrid).SelectedItem as myDataType ;
    if (tmp != null)
    {
       //Do your job here
       //tmp has edited data
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM