如何使用 MVVM 模式在 WPF 数据网格中绑定 CurrentCell

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/20080130/
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-09-13 10:04:38  来源:igfitidea点击:

How to Bind CurrentCell in WPF datagrid using MVVM pattern

c#wpfxamlmvvmdatagrid

提问by Bharath T

I'm learning WPF MVVM pattern. I'm stuck in Binding CurrentCellof datagrid. Basically I need the row index and column index of current cell.

我正在学习 WPF MVVM 模式。我被困在Binding CurrentCelldatagrid。基本上我需要当前单元格的行索引和列索引。

<DataGrid AutoGenerateColumns="True" 
          SelectionUnit="Cell" 
          CanUserDeleteRows="True" 
          ItemsSource="{Binding Results}" 
          CurrentCell="{Binding CellInfo}" 
          Height="282" 
          HorizontalAlignment="Left" 
          Margin="12,88,0,0" 
          Name="dataGrid1" 
          VerticalAlignment="Top" 
          Width="558" 
          SelectionMode="Single">

Here is my ViewModel

这是我的 ViewModel

private User procedureName = new User();

public  DataGridCell   CellInfo
{
    get { return procedureName.CellInfo; }
    //set
    //{
    //    procedureName.CellInfo = value;
    //    OnPropertyChanged("CellInfo");
    //}
}

Here is my Model

这是我的模型

private DataGridCell cellInfo;

public DataGridCell CellInfo
{
    get { return cellInfo; }
    //set
    //{
    //    cellInfo = value;
    //    OnPropertyChanged("CellInfo");
    //}
}

And in my ViewModel CellInfois always null. I am not able to get the value from the currentcellin datagrid. Please let me know a way to getCurrentCellin the ViewModel.

在我的 ViewModelCellInfo中总是null. 我无法从currentcellin 中获取值datagrid。请让我知道CurrentCell进入 ViewModel 的方法。

if (CellInfo != null)
{
    MessageBox.Show("Value is" + CellInfo.Column.DisplayIndex.ToString());
}

回答by kosdos

After having a quick poke-around I've noticed a very simple solution to your problem.

在快速浏览之后,我注意到您的问题有一个非常简单的解决方案。

First of all there's two problems rather then one here. You cannot bind a CellInfoof a type DataGridCell, it needs to be DataGridCellInfoas xaml cannot convert it on its own.

首先,这里有两个问题,而不是一个。你不能绑定 aCellInfo类型 DataGridCell,它需要是DataGridCellInfo因为 xaml 不能自己转换它。

Secondly in your xaml you will need to add Mode=OneWayToSourceor Mode=TwoWayto your CellInfobinding.

其次,在您的 xaml 中,您需要将Mode=OneWayToSource或添加Mode=TwoWay到您的CellInfo绑定中。

Here is a rough example semi-related to your original code

这是一个与您的原始代码半相关的粗略示例

XAML

XAML

<DataGrid AutoGenerateColumns="True"
          SelectionUnit="Cell"
          SelectionMode="Single"
          Height="250" Width="525" 
          ItemsSource="{Binding Results}"
          CurrentCell="{Binding CellInfo, Mode=OneWayToSource}"/>

VM

虚拟机

private DataGridCellInfo _cellInfo;
public DataGridCellInfo CellInfo
{
    get { return _cellInfo; }
    set
    {
        _cellInfo = value;
        OnPropertyChanged("CellInfo");
        MessageBox.Show(string.Format("Column: {0}",
                        _cellInfo.Column.DisplayIndex != null ? _cellInfo.Column.DisplayIndex.ToString() : "Index out of range!"));
    }
}

Just a small tip - if you debug your app and look at the Output window it actually tells you if there is any troubles with your bindings.

只是一个小技巧 - 如果您调试应用程序并查看“输出”窗口,它实际上会告诉您绑定是否存在任何问题。

Hope this helps!

希望这可以帮助!

K.

K。