【问题标题】:Why my datagrid not updates using MVVM为什么我的数据网格不使用 MVVM 更新
【发布时间】: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


【解决方案1】:

首先确保将 ViewModel 设置为窗口的 DataContext。您可以通过检查您创建的 Commands 在您单击 Save 按钮时是否被调用来进行测试。您可以查看此link 以进行验证。

还将 DGrid 更改为属性。因为目前它被定义为一个变量。改变

    public ObservableCollection<Model> DGrid;

    public ObservableCollection<Model> DGrid {get; set;}

这应该可行!希望对您有所帮助!

【讨论】:

  • 我把代码改成了这样:public ObservableCollection DGrid { get;放;但输出有点出乎意料,请参阅:我期待其他一些列更新,但它添加了一些其他 3 列而不是现有绑定的三列 prntscr.com/9vdbtf
  • 我也不明白为什么有 6 列,而不是 3 列
  • 您好,您可以将 DataGrid 的 AutoGenerateColumns 属性设置为 false 并修复列绑定,或者删除预定义的列绑定并允许 Grid 为您生成列。 XAML 中绑定的属性似乎与您在 Model 类中定义的不同。
  • @Andreia 谢谢,我从 xaml 中删除了 datagrid 的所有绑定,并且我将 AutoGenerateColumns 设置为 false,它不会提供空列,它不会添加数据进行研磨,也不会显示列的标题名称,请看prntscr.com/9vdja5
  • 哦抱歉删除列标题是我从 xaml 中删除的错误。对不起。
猜你喜欢
  • 1970-01-01
  • 2019-11-14
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多