【问题标题】:How to Bind CurrentCell in WPF datagrid using MVVM pattern如何使用 MVVM 模式在 WPF 数据网格中绑定 CurrentCell
【发布时间】:2013-12-03 12:17:51
【问题描述】:

我正在学习 WPF MVVM 模式。我被困在datagridBinding 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 User procedureName = new User();

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

这是我的模型

private DataGridCell cellInfo;

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

在我的 ViewModel 中,CellInfo 始终是 null。我无法从datagrid 中的currentcell 获取值。请告诉我在 ViewModel 中获取CurrentCell 的方法。

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

【问题讨论】:

  • CurrentCell="{Binding CellInfo}" - 尝试将这个东西设置为两种绑定模式。
  • 为什么你需要单元格信息,你不能绑定到数据网格的选定项并从那里提取你需要的属性吗?此外,从您的视图模型中调用消息框会产生问题。那只是为了测试?
  • 谢谢。我尝试使用 SelectedItem。在我的视图模型中甚至 selecteditem 都是空的。是的消息框仅用于测试。
  • @devhedgehog 我也尝试了两种模式。但我没有在我的视图模型中得到 CurrentCell。

标签: c# wpf xaml mvvm datagrid


【解决方案1】:

在快速浏览后,我发现了一个非常简单的解决您问题的方法。

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

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

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

XAML

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

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

希望这会有所帮助!

K.

【讨论】:

  • 非常感谢 kosdos。这和建议的效果一样好。我之前尝试过同样的事情,但是在测试时我尝试了按钮单击命令。所以每当我点击按钮时 CurrentCell 失去了焦点,这就是原因我总是为空。现在我通过在 set 内部测试得到了正确的值。也感谢您的提示。
  • @kosdos _cellInfo.Column.DisplayIndex != null ? _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 在绑定到数据网格的对象中的索引位置应与项的行,除非您正在应用自定义过滤、可视化或类似逻辑。这些是我此刻突然想到的一些,希望对您有所帮助!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-01-29
相关资源
最近更新 更多