简体   繁体   English

WPF DataGrid Navision 功能

[英]WPF DataGrid Navision functionality

I'm working on creating a DataGrid with the same functionality as Microsoft Dynamic Nav .我正在创建一个与Microsoft Dynamic Nav具有相同功能的DataGrid
This is almost like a Excel spreadsheet.这几乎就像一个 Excel 电子表格。

The idea is that you can start editing a cell.这个想法是您可以开始编辑单元格。 After you are done there are some steps:完成后有一些步骤:

  1. The content of the cell is validated against a list.单元格的内容根据列表进行验证。
    1.1 If the list contains the given string (Not case sensitive), then the content in the DataGrid is replaced with the string from the list (aka the one where the case is right). 1.1 如果列表包含给定的字符串(不区分大小写),则 DataGrid 中的内容将替换为列表中的字符串(也就是大小写正确的字符串)。
    1.2 If the list doesn't contain the string it will show a new form with possibilities, and then use the one that the user selects to replace the one already typed into the DataGrid. 1.2 如果列表中不包含字符串,它将显示一个具有可能性的新表单,然后使用用户选择的表单替换已经输入到 DataGrid 中的表单。

  2. If the user has typed something right or selected a correct value from the list, then the DataGrid will automatic fill some columns with pre-set string如果用户输入了正确的内容或从列表中选择了正确的值,则 DataGrid 将自动使用预设字符串填充某些列

If we look at an example: If the user types z in column 1 then i want column 2 to become "x" and the focus to move to colum3如果我们看一个例子:如果用户在第 1 列中键入 z,那么我希望第 2 列变为“x”并且焦点移到第 3 列

DataGrid: (Before enter/Tab pressed)

[Column1] [Column2] [Column3] [Column4]
[  "a"  ] [  "b"  ] [  "c"  ] [  "d"  ]
[  "z"  ] [       ] [       ] [       ]
(focused)

DataGrid: (Afture enter/tab pressed
[Column1] [Column2] [Column3] [Column4]
[  "a"  ] [  "b"  ] [  "c"  ] [  "d"  ]
[  "z"  ] [  "x"  ] [       ] [       ]
                    (focused)

Here I encounter some problems:这里我遇到了一些问题:

  1. I use the CellEditEnding event to validate the text input.我使用CellEditEnding事件来验证文本输入。 If I find the correct text then I edit the currentItem at the correct variable.如果我找到正确的文本,那么我在正确的变量处编辑currentItem But now I need to call dataGrid1.Items.Refresh();但现在我需要调用dataGrid1.Items.Refresh(); and than can't be done while in edit mode.并且在编辑模式下无法完成。

MyCode:我的代码:

FormTest.xaml FormTest.xaml

...
<DataGrid.Columns>
    <DataGridTextColumn Header="C1" 
        Binding="{Binding c1,UpdateSourceTrigger=LostFocus}"/>
    <DataGridTextColumn Header="C2" 
        Binding="{Binding c2,UpdateSourceTrigger=LostFocus}"/>
    <DataGridTextColumn Header="C3" 
        Binding="{Binding c3,UpdateSourceTrigger=LostFocus}"/>
    <DataGridTextColumn Header="C4" 
        Binding="{Binding c4,UpdateSourceTrigger=LostFocus}"/>
    <DataGridTextColumn Header="C5" 
        Binding="{Binding c5,UpdateSourceTrigger=LostFocus}"/>
</DataGrid.Columns>
...

FormTest.xaml.cs FormTest.xaml.cs

public FormTest()
{
    InitializeComponent();
    loadMockData();
}
private void loadMockData()
{
    dataItems = new DataItems();
    dataItems.Add(new DataItem() { c1 = "a", c2 = "b", c3 = "c", c4 = "d", c5 = "e" });
    dataItems.Add(new DataItem() { c1 = "a", c2 = "b", c3 = "c", c4 = "d", c5 = "e" });
    dataItems.Add(new DataItem() { c1 = "a", c2 = "b", c3 = "c", c4 = "d", c5 = "e" });
    dataItems.Add(new DataItem() { c1 = "a", c2 = "b", c3 = "c", c4 = "d", c5 = "e" });
    dataItems.Add(new DataItem() { c1 = "a", c2 = "b", c3 = "c", c4 = "d", c5 = "e" });

    dataGrid1.ItemsSource = dataItems;
}
private void dataGrid1_CellEditEnding(object sender, DataGridCellEditEndingEventArgs e)
{
    TextBox editElement = e.EditingElement as TextBox;
    DataItem di = dataGrid1.CurrentItem as DataItem;
    DataGridCellInfo cell = dataGrid1.CurrentCell;
    if (e.Column.DisplayIndex == 0)
    {
        if (editElement.Text == "z")
        {
            editElement.Text = "Z"; 
            di.c1 = "Z";
            di.c2 = "X";
        }
    }
}
private void dataGrid1_PreviewKeyDown(object sender, KeyEventArgs e)
{
    if (e.Key == Key.Enter || e.Key == Key.Tab)
    {
        dataGrid1.CommitEdit();
        DataItem di = dataGrid1.CurrentItem as DataItem;
        dataGrid1.CancelEdit();
        dataGrid1.Items.Refresh();
     }
}

private class DataItems : List<DataItem> { }
private class DataItem
{
    public int recID { get; set; }
    public String c1 { get; set; }
    public String c2 { get; set; }
    public String c3 { get; set; }
    public String c4 { get; set; }
    public String c5 { get; set; }
}

The problems I encounter in this code are:我在这段代码中遇到的问题是:

  1. After the refresh is called, there is no selected cell.调用刷新后,没有选定的单元格。 That means it is not possible to continue entering data without using the mouse to select a cell.这意味着在不使用鼠标 select 单元格的情况下,无法继续输入数据。

  2. When I start typing a new item (aka the empty row), and press enter or tab, the dataGrid1.CancelEnding() will remove the new item, and the row is empty again.当我开始输入一个新项目(又名空行)并按 Enter 或 Tab 键时, dataGrid1.CancelEnding()将删除新项目,该行再次为空。


The Questions问题

  • Is it possible to edit the content of the datagrid (The ItemSoure and what is displayed) without calling Items.Refresh() ?是否可以在不调用Items.Refresh()的情况下编辑数据网格的内容( ItemSoure和显示的内容)?
  • If not is it possible to set a cell focus after the Items.Refresh() is done.如果没有,可以在Items.Refresh()完成后设置单元格焦点。 ? ?
  • Or is there a better way of doing this?或者有没有更好的方法来做到这一点?

I can answer your question about updating the content of the grid.我可以回答您有关更新网格内容的问题。

Normally, it's not necessary to refresh the entire grid following a change of one property.通常,没有必要在更改一个属性后刷新整个网格。 (It's also rather wasteful if you've got a lot of data in your grid.) If your class DataItem implements the INotifyPropertyChanged interface, and you fire a PropertyChanged event every time the value of one of its properties changes, WPF will update the data in the table automatically. (如果您的网格中有大量数据,这也是相当浪费的。)如果您的 class DataItem实现了INotifyPropertyChanged接口,并且每次其属性之一的值发生更改时触发PropertyChanged事件,WPF 将更新数据自动在表中。

This page on MSDN contains an example of implementing this interface, and you can find a more complete example of the use of this interface here . MSDN 上的这个页面包含一个实现该接口的示例,您可以在此处找到更完整的该接口使用示例。

This should get rid of the need to use the dataGrid1_PreviewKeyDown event handler, which in turn gets rid of the problem with not being able to add a new row to the bottom of the grid.这应该消除使用dataGrid1_PreviewKeyDown事件处理程序的需要,从而消除了无法在网格底部添加新行的问题。

You should also add Mode=TwoWay to the Binding s in your DataGridTextColumn s.您还应该将Mode=TwoWay添加到DataGridTextColumnBinding中。 Otherwise, when you edit a cell, WPF won't update the relevant DataItem object with the values you enter in the grid.否则,当您编辑单元格时,WPF 不会使用您在网格中输入的值更新相关的DataItem object。

I had a look at a number of approaches to 'tab' over the second column after entering a value in the first column, but alas I didn't manage to find anything that worked.在第一列中输入值后,我查看了在第二列上“制表符”的多种方法,但可惜我没有找到任何有效的方法。

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

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