将GridView绑定到列表

时间:2013-02-27 14:52:28

标签: c# wpf xaml data-binding mvvm

我遇到了最糟糕的时间。我必须使用我还不完全理解的MVVM,并将填充了SQL数据的列表绑定到radGridView。听起来很容易......但我无法弄明白。数据没有显示,我认为这是因为我绑定它错了。以下是我的相关代码。任何帮助将不胜感激!

C#(HistoryAuditLogViewModel.cs):

  #region Private Fields

        private DatabaseConnectionSetting dbSetting;
        private string tableName = "Manufacturers";
        private int primaryKeyID = 1;
        private string entryID;
        private string manufacturerID;
        private string manufacturerName;
        private string auditDate;
        private string sqlLogin;
        private string application;


        private string dbConnectionKey = Alliance.Infrastructure.Common.DatabaseConnectionSetting.BACKFLOW_SCOPE_KEY;

        #endregion

 public void Load_Audit()
        {
            string strSQLconnection = (dbSetting.SqlConnectionString + "; User Id = " + dbSetting.SqlUserName + "; Password = " + dbSetting.SqlPassword + ";");
            SqlConnection sqlConnection = new SqlConnection(strSQLconnection); 

            sqlConnection.Open();
            SqlCommand sqlCommand = new SqlCommand("SELECT [EntryID], [AuditValue].value('(row/@ManufacturerID)[1]', 'int') as ManufacturerID, [AuditValue] .value('(row/@ManufacturerName)[1]', 'nvarchar(50)') as ManufacturerName, [AuditDate], [SqlLogin], [Application] from [Backflow].[dbo].[AuditLog] where (TableName = @tableName AND [EntryID] = @primarykey)", sqlConnection);
            sqlCommand.Parameters.AddWithValue("@tablename", tableName);
            sqlCommand.Parameters.AddWithValue("@primarykey", primaryKeyID);

            SqlDataReader reader = sqlCommand.ExecuteReader();



            List<String> dataList = new List<String>();


            while (reader.Read())
                for (int i = 0; i < reader.FieldCount; i++)
                {
                    string rdr = reader[i].ToString();

                    dataList.Add(rdr);
                }
        }

XAML(HistoryAuditLogView.xaml):

 <telerik:RadGridView Name="AuditGrid" ItemsSource="{Binding dataList}">

        </telerik:RadGridView>

4 个答案:

答案 0 :(得分:2)

List<String> dataList = new List<String>();

它只是一个局部变量,只在该方法中可访问且有效。您必须将其移动到属性或字段中:

public List<String> DataList { get; set; }

并在方法中:

DataList= new List<String>();

while (reader.Read())
    for (int i = 0; i < reader.FieldCount; i++)
    {
        string rdr = reader[i].ToString();
        DataList.Add(rdr);
    }

答案 1 :(得分:2)

完整的公共财产可以帮助您,在ViewModel

的根级添加它
#region Private Fields
...
#region
#region Public Props

private List<String> _dataList;
public List<String> dataList 
{
get{ return _datalist;}
set{_datalist = value;}
}

#region


public void Load_Audit()
        {
dataList = new List<String>(); //then your code
//.... 
}

答案 2 :(得分:1)

MVVM Bindings与ViewModel中的公共属性一起使用...

此外,如果可能的话,最好绑定到Observable Collections。

为此,您需要使用以下代码;

private ObservableCollection<String> _DataList;
public ObservableCollection<String> DataList {
    get { return _DataList; }
    set {
        if (value.Equals(_Details) == false) {
                 _DataList= value;
                  OnPropertyChanged("DataList");
        }
    }
}

OnPropertyChanged Sub会引发INotifiyPropertyChanged事件,因此对Collection的任何更改都会自动反映在GridView中。

您需要创建此子,但是如果您愿意,可以直接在Setter中引发此事件。

您的准则将是;

_Details = new ObserservableCollection<string>();

while (reader.Read())
   for (int i = 0; i < reader.FieldCount; i++)
   {
       string rdr = reader[i].ToString();
       dataList.Add(rdr);
   }

您的XAML将保持与上述相同。

答案 3 :(得分:0)

尝试在初始化View和viewModel

时设置DataContext
MyView view = new MyView();
MyViewModel viewModelodel = new MyViewModel();

view.DataContext = viewModel;

另一种解决方案是在View中设置DataContex。 但是你必须告诉你的View ViewModels位置。

<Window x:Class="MyNamespace.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="clr-namespace:MyNamespace"
    Title="MainWindow" Height="Auto" Width="Auto" >
<Window.DataContext>
    <local:MyViewModel/>
</Window.DataContext>
</Window>

当然,您必须在ViewModel中添加一个代表您的列表的公共属性。