如何使用MVVM模式在WPF datagrid中绑定CurrentCell [英] How to Bind CurrentCell in WPF datagrid using MVVM pattern

查看:652
本文介绍了如何使用MVVM模式在WPF datagrid中绑定CurrentCell的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

 < DataGrid AutoGenerateColumns =True
SelectionUnit =Cell
CanUserDeleteRows =True
ItemsSource ={Binding Results}
CurrentCell ={Binding CellInfo}
Height =282
Horizo​​ntalAlignment =Left
Margin =12,88,0,0
Name =dataGrid1
VerticalAlignment =Top
宽度=558
SelectionMode =Single>

这是我的ViewModel

  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());
}


解决方案

周围我注意到了一个非常简单的解决方案来解决您的问题。



首先,有两个问题,而不是一个。您不能绑定类型为 DataGridCell CellInfo ,则需要 DataGridCellInfo 因为xaml不能自己转换。



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



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



XAML

 < DataGrid AutoGenerateColumns =True
SelectionUnit =Cell
SelectionMode =Single
Height =250Width =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():索引超出范围! ));
}
}

只是一个小小的提示 - 如果你调试你的应用程序看看输出窗口,它实际上告诉你绑定是否有麻烦。



希望这有帮助!



K。


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

<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

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 CellInfo is always null. I am not able to get the value from the currentcell in datagrid. Please let me know a way to getCurrentCell in the ViewModel.

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

解决方案

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 CellInfo of a type DataGridCell, it needs to be DataGridCellInfo as xaml cannot convert it on its own.

Secondly in your xaml you will need to add Mode=OneWayToSource or Mode=TwoWay to your CellInfo binding.

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

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.

这篇关于如何使用MVVM模式在WPF datagrid中绑定CurrentCell的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆