【发布时间】:2016-05-03 22:54:16
【问题描述】:
我的数据绑定有什么问题?
我是 MVVM 初学者,我必须在文本框中从用户那里获取一些数据,然后在数据网格中显示该数据,分别有 3 个文本框和相应的 3 个文本框。 还有一个按钮,点击后必须将输入的数据保存到数据网格列中。
除了数据没有更新到数据网格之外,我一切正常。
这是我的观点:
< Window x: Class = "DatagRidBelowTextUpdate.MainWindow"
xmlns = "http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns: x = "http://schemas.microsoft.com/winfx/2006/xaml"
Title = "MainWindow"
Height = "350"
Width = "525" >
< Grid >
< Grid.RowDefinitions >
< RowDefinition > < /RowDefinition> < RowDefinition Height = "30" > < /RowDefinition> < RowDefinition > < /RowDefinition> < /Grid.RowDefinitions> < Grid Grid.Row = "0" >
< Grid.RowDefinitions >
< RowDefinition > < /RowDefinition> < RowDefinition > < /RowDefinition> < RowDefinition > < /RowDefinition> < /Grid.RowDefinitions> < Grid.ColumnDefinitions >
< ColumnDefinition > < /ColumnDefinition> < ColumnDefinition > < /ColumnDefinition> < /Grid.ColumnDefinitions> < TextBox Grid.Column = "1"
Grid.Row = "0"
Text = "{Binding EditModel.TextName}"
Height = "20"
Width = "80"
HorizontalAlignment = "Center" > < /TextBox> < TextBox Grid.Column = "1"
Grid.Row = "1"
Text = "{Binding EditModel.RollNumber}"
Height = "20"
Width = "80" > < /TextBox> < TextBox Grid.Column = "1"
Grid.Row = "2"
Text = "{Binding EditModel.Class}"
Height = "20"
Width = "80" > < /TextBox> < Label Grid.Row = "0"
HorizontalAlignment = "Center"
VerticalAlignment = "Center" > Name < /Label> < Label Grid.Row = "1"
HorizontalAlignment = "Center"
VerticalAlignment = "Center" > RollNumber < /Label> < Label Grid.Row = "2"
HorizontalAlignment = "Center"
VerticalAlignment = "Center" > Class < /Label> < /Grid> < Grid Grid.Row = "1" >
< Button Width = "80"
Height = "20"
Command = "{Binding SaveStudentRecord}" > Save < /Button> < /Grid> < Grid Grid.Row = "2" >
< DataGrid ItemsSource = "{Binding DGrid}" >
< DataGrid.Columns >
< DataGridTextColumn Header = "Name"
Binding = "{Binding DgName, Mode=TwoWay}"
Width = "150" > < /DataGridTextColumn> < DataGridTextColumn Header = "Rollnumber"
Binding = "{Binding DgRollnumber, Mode=TwoWay}"
Width = "150" > < /DataGridTextColumn> < DataGridTextColumn Header = "Class"
Binding = "{Binding DgClass , Mode=TwoWay}"
Width = "150" > < /DataGridTextColumn> < /DataGrid.Columns> < /DataGrid> < /Grid> < /Grid> < /Window>
我的视图模型:
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Windows;
namespace DatagRidBelowTextUpdate {
class ViewModel {
private RelayCommand saveStudentRecord;
private Model editModel;
public bool canExecute {
get;
set;
}
public ObservableCollection < Model > DGrid;
private string dgName;
private string dgRollnumber;
private string dgClass;
public string DgName {
get {
return dgName;
}
set {
dgName = value;
PropertyChangedEventArgs("DgName");
}
}
public string DgRollnumber {
get {
return dgRollnumber;
}
set {
dgRollnumber = value;
PropertyChangedEventArgs("DgRollnumber");
}
}
public string DgClass {
get {
return dgClass;
}
set {
dgClass = value;
PropertyChangedEventArgs("DgClass");
}
}
public Model EditModel {
get {
return editModel;
}
set {
editModel = value;
PropertyChangedEventArgs("EditModel");
}
}
public ViewModel() {
editModel = new Model();
canExecute = true;
DGrid = new ObservableCollection < Model > ();
}
public RelayCommand SaveStudentRecord {
get {
return saveStudentRecord = new RelayCommand(() => MyAction(), canExecute);
}
}
private void MyAction() {
string chck1 = editModel.TextName; //TextName; //I see on debugging that TextName contains the text entered so how to add this text to Datagrid column
string chck2 = editModel.Class;
string chck3 = editModel.RollNumber;
// DGrid = new ObservableCollection<Model>();
// dgClass = editModel.Class;
// dgName = editModel.TextName;
// dgRollnumber = editModel.RollNumber;
DGrid.Add(editModel);
// editModel = new Model();
}
public event PropertyChangedEventHandler PropertyChanged;
private void PropertyChangedEventArgs(string propertyName) {
if (PropertyChanged != null) {
PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
}
}
}
}
我的模特:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
namespace DatagRidBelowTextUpdate {
class Model {
private string textName;
private string rollNumber;
private string cclass;
public string TextName {
get {
return textName;
}
set {
textName = value;
PropertyChangedEventArgs("TextName");
}
}
public string RollNumber {
get {
return rollNumber;
}
set {
rollNumber = value;
PropertyChangedEventArgs("RollNumber");
}
}
public string Class {
get {
return cclass;
}
set {
cclass = value;
PropertyChangedEventArgs("Class");
}
}
public event PropertyChangedEventHandler PropertyChanged;
private void PropertyChangedEventArgs(string propertyName) {
if (PropertyChanged != null) {
PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
}
}
}
}
绑定没有更新按钮单击MyAction()中的数据网格有什么问题
编辑:
(我不想使用 DGrid.Add(editModel); 并且我不想将 AutoGenrateColumns 设置为 True,因为必须了解单个 UI 的绑定概念)。我的意思是我希望在 xaml 中手动绑定 datagrid 中的列和然后使用文本框绑定变量将文本框数据分配给数据网格绑定变量,我的意思是像dgName=editModel.Name 按钮单击。
Viem 模型是:
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Windows;
namespace DatagRidBelowTextUpdate
{
class ViewModel :INotifyPropertyChanged
{
private RelayCommand saveStudentRecord;
private Model editModel;
public bool canExecute { get; set; }
public ObservableCollection<Model> DGrid { get; set; }
private string dgName{ get; set; }
private string dgRollnumber { get; set; }
private string dgClass { get; set; }
public string DgName
{
get
{
return dgName;
}
set
{
dgName = value;
PropertyChangedEventArgs("DgName");
}
}
public string DgRollnumber
{
get
{
return dgRollnumber;
}
set
{
dgRollnumber = value;
PropertyChangedEventArgs("DgRollnumber");
}
}
public string DgClass
{
get
{
return dgClass;
}
set
{
dgClass = value;
PropertyChangedEventArgs("DgClass");
}
}
public Model EditModel
{
get
{
return editModel;
}
set
{
editModel = value;
PropertyChangedEventArgs("EditModel");
}
}
public ViewModel()
{
editModel = new Model();
canExecute = true;
}
public RelayCommand SaveStudentRecord
{
get { return saveStudentRecord = new RelayCommand(() => MyAction(), canExecute); }
}
private void MyAction()
{
dgClass = editModel.Class;
dgName = editModel.TextName;
dgRollnumber = editModel.RollNumber;
//according to my understanding , the data in editModel.Class must be added to dgClass in View, obsolutley i am wrong, please correct me.
}
public event PropertyChangedEventHandler PropertyChanged;
private void PropertyChangedEventArgs(string propertyName)
{
if (PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
}
}
}
}
视图是:
<Window x:Class="DatagRidBelowTextUpdate.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="350" Width="525">
<Grid>
<Grid.RowDefinitions>
<RowDefinition></RowDefinition>
<RowDefinition Height="30"></RowDefinition>
<RowDefinition></RowDefinition>
</Grid.RowDefinitions>
<Grid Grid.Row="0">
<Grid.RowDefinitions>
<RowDefinition></RowDefinition>
<RowDefinition></RowDefinition>
<RowDefinition></RowDefinition>
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition></ColumnDefinition>
<ColumnDefinition></ColumnDefinition>
</Grid.ColumnDefinitions>
<TextBox Grid.Column="1" Grid.Row="0" Text="{Binding EditModel.TextName}" Height="20" Width="80" HorizontalAlignment="Center"></TextBox>
<TextBox Grid.Column="1" Grid.Row="1" Text="{Binding EditModel.RollNumber}" Height="20" Width="80"></TextBox>
<TextBox Grid.Column="1" Grid.Row="2" Text="{Binding EditModel.Class}" Height="20" Width="80"></TextBox>
<Label Grid.Row="0" HorizontalAlignment="Center" VerticalAlignment="Center">Name</Label>
<Label Grid.Row="1" HorizontalAlignment="Center" VerticalAlignment="Center">RollNumber</Label>
<Label Grid.Row="2" HorizontalAlignment="Center" VerticalAlignment="Center">Class</Label>
</Grid>
<Grid Grid.Row="1" >
<Button Width="80" Height="20" Command="{Binding SaveStudentRecord}">Save</Button>
</Grid>
<Grid Grid.Row="2">
<DataGrid ItemsSource="{Binding DGrid, Mode=TwoWay}" AutoGenerateColumns="False">
<DataGrid.Columns>
<DataGridTextColumn Header="Name" Binding="{Binding DgName}" Width="150"></DataGridTextColumn>
<DataGridTextColumn Header="Rollnumber" Binding="{Binding DgRollnumber}" Width="150"></DataGridTextColumn>
<DataGridTextColumn Header="Class" Binding="{Binding DgClass}" Width="150"></DataGridTextColumn>
</DataGrid.Columns>
</DataGrid>
</Grid>
</Grid>
</Window>
【问题讨论】:
-
一件事是您正在实现 INotifyPropertyChanged 但您没有在视图模型中继承它,因此通知不起作用。
-
@JanneMatikainen 谢谢,但我刚试过,还是不行。
标签: c# wpf mvvm data-binding datagrid