Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/274.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何使用MVVM模式绑定WPF数据网格中的CurrentCell_C#_Wpf_Xaml_Mvvm_Datagrid - Fatal编程技术网

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

C# 如何使用MVVM模式绑定WPF数据网格中的CurrentCell,c#,wpf,xaml,mvvm,datagrid,C#,Wpf,Xaml,Mvvm,Datagrid,我正在学习WPF MVVM模式。我被困在datagrid的Binding CurrentCell中。基本上我需要当前单元格的行索引和列索引 <DataGrid AutoGenerateColumns="True" SelectionUnit="Cell" CanUserDeleteRows="True" ItemsSource="{Binding Results}" CurrentCell="{Bind

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

<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">
这是我的模型

private DataGridCell cellInfo;

public DataGridCell CellInfo
{
    get { return cellInfo; }
    //set
    //{
    //    cellInfo = value;
    //    OnPropertyChanged("CellInfo");
    //}
}
在我的视图中,model
CellInfo
总是
null
。我无法从
datagrid
中的
currentcell
获取值。请告诉我如何在ViewModel中获取
CurrentCell

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

在快速浏览之后,我发现了一个非常简单的解决方案

首先,这里有两个问题,而不是一个。无法绑定类型为
DataGridCell
CellInfo
,它必须是
DataGridCellInfo
,因为xaml无法自行转换它

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

下面是一个与原始代码相关的粗略示例

XAML

只是一个小提示-如果您调试应用程序并查看输出窗口,它实际上会告诉您绑定是否有任何问题

希望这有帮助


K.

CurrentCell=“{Binding CellInfo}”-尝试将此设置为双向绑定模式。为什么需要单元格信息,不能绑定到数据网格的选定项并从中提取所需的属性?此外,从viewmodel调用messagebox也会产生问题。这只是为了测试吗?谢谢。我尝试了SelectedItem。甚至SelectedItem在我的视图模型中也是空的。是的messagebox只是为了测试。@devhedgehog我也尝试了双向模式。但是我在视图模型中没有得到CurrentCell。非常感谢kosdos。这很好。我以前也尝试过同样的方法,但在测试过程中,我尝试使用butoon click命令。因此,每当我单击按钮CurrentCell时,它都会失去焦点,这就是我始终为空的原因。现在,我通过在集合内部测试得到了正确的值。也感谢您的提示。@kosdos
\u cellInfo.Column.DisplayIndex!=空的_cellInfo.Column.DisplayIndex.ToString()
总是正确的。顺便说一句,如何获取所选单元格的行索引?@Chenxiao提取行索引的方法有多种,这里有几种:-
DataGrid
包含一个名为
SelectedIndex
的属性,如果这对您正确的话(如果在切换行时使用,就我所记得的情况而言,这是非常挑剔的)-非MVVM方法包括:
int rowIndex=dataGrid1.Items.IndexOf(cell.Item);
var currentRowIndex=my_dataGrid.Items.IndexOf(my_dataGrid.CurrentItem);
-如果是MVVM方法:
SelectedItem
在绑定到datagrid的对象中的索引位置应该与项目的行相同,除非您应用自定义筛选、可视化或模拟逻辑。这些是此时突然出现在我脑海中的一些,希望能有所帮助!
if (CellInfo != null)
{
    MessageBox.Show("Value is" + CellInfo.Column.DisplayIndex.ToString());
}
<DataGrid AutoGenerateColumns="True"
          SelectionUnit="Cell"
          SelectionMode="Single"
          Height="250" Width="525" 
          ItemsSource="{Binding Results}"
          CurrentCell="{Binding CellInfo, Mode=OneWayToSource}"/>
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!"));
    }
}