wpf - 无法将 observablecollection 绑定(bind)到 MVVM 中的数据网格

标签 wpf binding mvvm datagrid

我对 MVVM 完全陌生。我试图将 sqlserver 中的数据绑定(bind)到 WPF 中的 Datagrid 并对其执行编辑、更新和删除操作。
现在,我无法使用 MVVM 中的 observable 集合将数据从 sqlserver 绑定(bind)到 datagrid 至少......
有人请帮我解决它,也请让我知道如何为同一个数据网格实现编辑、更新和删除操作......我完全对实现 MVVM 架构感到困惑..

使用以下代码,我可以将数据绑定(bind)到“Empdata”(observablecollection),但根本没有显示数据网格。

以下是我的 xaml 代码:

<DataGrid    ItemsSource="{Binding Path=Empdata}" x:Name="dtgrdemp"
             AutoGenerateColumns="False"
             SelectionMode="Single"
             SelectionUnit="FullRow"
             GridLinesVisibility="Horizontal"
             CanUserDeleteRows="True"
             CanUserAddRows="False">
    <DataGrid.Columns>
        <DataGridTextColumn Header="Name" Width="SizeToCells" MinWidth="125" Binding="{Binding Path=Ename}"/>
        <DataGridTextColumn Header="Age" Width="SizeToCells" MinWidth="200" Binding="{Binding Path=Eage}"/>
        <DataGridTextColumn Header="Description" Width="SizeToCells" MinWidth="200" Binding="{Binding Path=Edescription}"/>
    </DataGrid.Columns></Datagrid>

以下是我的“ View ”代码,我以人的身份上课
public class Person : INotifyPropertyChanged, IDataErrorInfo
{
    private string names;

    public string Names
    {
        get { return names; }
        set
        {
            names = value;
            OnPropertyChanged("Names");
        }
    }

    private int age;

    public int Age
    {
        get { return age; }
        set
        {
            age = value;
            OnPropertyChanged("Age");
        }
    }

    private string description;

    public string Description
    {
        get { return description; }
        set
        {
            description = value;
            OnPropertyChanged("Description");
        }
    }



    public event PropertyChangedEventHandler PropertyChanged;

    protected void OnPropertyChanged(string propertyname)
    {
        var handler = PropertyChanged;
        if (handler != null)
            handler(this, new PropertyChangedEventArgs(propertyname));
    }

    public string Error
    {
        get { return null; }
    }

    public string this[string columnName]
    {
        get
        {
            string error = null;
            switch (columnName)
            {
                case "Names":
                    if (string.IsNullOrEmpty(names))
                    {
                        error = "First Name required";
                    }
                    break;

                case "Age":
                    if ((age < 18) || (age > 85))
                    {
                        error = "Age out of range.";
                    }
                    break;
                case "Description":
                    if (string.IsNullOrEmpty(description))
                    {
                        error = "Last Name required";
                    }
                    break;
            }

            return (error);
        }
    }

以下是我的“ViewModel”类代码
public class MainViewModel :INotifyPropertyChanged
{
    string con = ConfigurationSettings.AppSettings["ConnectionStrings"];
    ObservableCollection<EmpInfo> Empdata= new ObservableCollection<EmpInfo>();

    private Person empperson;

    public Person Empperson
    {
        get { return empperson; }
        set { empperson = value; }
    }

    public MainViewModel()
    {
        initializeload();
    }

    private void initializeload()
    {
        DataTable dt = new DataTable();
        Empdata = new ObservableCollection<EmpInfo>();
        Empdata.Add(new EmpInfo(dt));
    }

    public event PropertyChangedEventHandler PropertyChanged;
    protected void OnPropertyChanged(string propertyname)
    {
        var handler = PropertyChanged;
        if (handler != null)
            handler(this, new PropertyChangedEventArgs(propertyname));
    }
}

以下是 EmpInfo.cs 类的代码
public EmpInfo(DataTable dt)
{
    SqlConnection sqlcon = new SqlConnection(con);
    sqlcon.Open();
    SqlDataAdapter da = new SqlDataAdapter("Select Ename,Eage,Edescription from EmployeeTable", sqlcon);
    da.Fill(dt);
    this.dt = dt;
}

以下是 Appxaml.cs 中的代码
protected override void OnStartup(StartupEventArgs e)
{
    base.OnStartup(e);
    var mainWindow = new MainWindow();
    var viewModel = new MainViewModel();
    mainWindow.DataContext = viewModel;
    mainWindow.Show();
}

最佳答案

您的代码 Empdata.Add(new EmpInfo(dt));未填充可观察集合 Empdata什么都有!

至少这就是你的构造函数

   public EmpInfo(DataTable dt)  

确认。按年龄、描述和姓名填充员工对象的代码在哪里?

关于wpf - 无法将 observablecollection 绑定(bind)到 MVVM 中的数据网格,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7401518/

相关文章:

c# - WPF、MVVM、NUnit 和线程……我做错了吗

c# - 设置组合框的选定值

wpf - 调试 Visual Studio 或 Blend Silverlight/WPF 设计器加载错误?

c# - C#WPF调度程序线程

wpf - 在整个应用程序中更改 FocusVisualStyle

wpf - 标记扩展中的 DepedencyProperty

cocoa - 验证 TextField 中的文本

c# - BindingContext 和多选数据 GridView

wpf - 为什么这个标签在绑定(bind)时看起来与静态值不同?

c# - 使用 MVVM 跨多个子控件的 WPF UserControl 自定义粘贴命令