0

我正在最糟糕的时候弄清楚这一点。我必须使用我还不完全理解的 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

4 回答 4

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);
    }
于 2013-02-27T14:54:33.980 回答
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
//.... 
}
于 2013-02-27T15:10:04.270 回答
1

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

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

For this, you would need to use the following code;

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

The OnPropertyChanged Sub raises the INotifiyPropertyChanged event, so that any changes to the Collection are automatically reflected in your GridView.

You would need to create this sub, however if you wanted to, you could raise this event directly in the Setter.

Your Code would then be;

_Details = new ObserservableCollection<string>();

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

Your XAML would remain the same as you have above.

于 2013-02-27T15:14:34.353 回答
0

Try to set the DataContext when you Initialize the View an the viewModel

MyView view = new MyView();
MyViewModel viewModelodel = new MyViewModel();

view.DataContext = viewModel;

An other solution would be to set the DataContex in your View. But you have to tell your View the ViewModels location.

<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>

Of course you have to add a public Property which represents your list in your ViewModel.

于 2013-03-01T15:30:25.897 回答