如何使用MVVM模式来绑定CurrentCell在WPF数据网格 [英] How to Bind CurrentCell in WPF datagrid using MVVM pattern

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

问题描述

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

 <数据网格的AutoGenerateColumns =真
SelectionUnit =单元格
CanUserDeleteRows =真
的ItemsSource ={结合的结果}
CurrentCell ={结合CellInfo}
HEIGHT =282
的Horizo​​ntalAlignment =左
保证金=12,88,0,0
NAME =DataGrid1中
VerticalAlignment =评出的
WIDTH =558
=的SelectionMode单>

下面是我的ViewModel

 私人用户PROCEDURENAME =新用户(); 

公共DataGridCell CellInfo
{
得到
{
返回procedureName.CellInfo;
}
//设置
// {
// procedureName.CellInfo =价值;
// OnPropertyChanged(CellInfo);
//}
}

下面是我的模型



 私人DataGridCell cellInfo; 

公共DataGridCell CellInfo
{
得到
{
返回cellInfo;
}
//设置
// {
// cellInfo =价值;
// OnPropertyChanged(CellInfo);
//}
}



在我的ViewModel CellInfo 总是。我不能够在的DataGrid 来获得从 currentcell 的值。请让我知道一种方法的视图模型来获得 CurrentCell

 如果(!CellInfo = NULL)
{
MessageBox.Show(价值是+ CellInfo.Column.DisplayIndex.ToString());
}


解决方案

有一个快速的poke-后围绕我注意到一个非常简单的解决您的问题。

所有的

首先有两个问题而不是一个在这里。你不能绑定 CellInfo A型 DataGridCell ,它需要的 DataGridCellInfo 为XAML不能转换它自己的。



其次在XAML中,你将需要添加模式= OneWayToSource 模式=双向你的 CellInfo 绑定。



下面是一个粗略的例子半与你的原代码



XAML

 <数据网格的AutoGenerateColumns =真
SelectionUnit =单元格
的SelectionMode =单
HEIGHT =250WIDTH =525
的ItemsSource ={结合的结果}
CurrentCell ={结合CellInfo,模式= OneWayToSource}/>



VM

 私人DataGridCellInfo _cellInfo; 
公共DataGridCellInfo CellInfo
{
{返回_cellInfo; }

{
_cellInfo =价值;
OnPropertyChanged(CellInfo);
MessageBox.Show(的String.Format(列:{0},
_cellInfo.Column.DisplayIndex = NULL _cellInfo.Column.DisplayIndex.ToString():!?索引超出范围! ));
}
}



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



希望这有助于!



ķ。


I am learning WPF MVVM pattern. I am 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模式来绑定CurrentCell在WPF数据网格的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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