我有两个名为 Breed 和 PetType 的类,这些类也是我的sql server数据库中的表
品种类是:
public int Id { get; set; }
public string BreedName { get; set; }
public string Description { get; set; }
public int SortOrder { get; set; }
public bool StatusId { get; set; }
public int CreatedBy { get; set; }
public string CreatedByName { get; set; }
public int PetTypeId { get; set; } //Inner or left joined from PetTypes Table(stored procedure)
public string PetTypeName { get; set; }
PetType 类是:
public int Id { get; set; }
public string PetTypeName { get; set; }
public string Description { get; set; }
public int SortOrder { get; set; }
public bool StatusId { get; set; }
public int CreatedBy { get; set; }
public string CreatedByName { get; set; }
然后在我的DataAccessLayer项目中,
public List<Breed> RetrieveMany(Breed parameters)
{
var items = new List<Breed>();
var command = new SqlCommand {CommandText = "SP_RetrieveBreeds", CommandType = CommandType.StoredProcedure};
command.Parameters.AddWithValue("@Id", parameters.Id).Direction = ParameterDirection.Input;
command.Parameters.AddWithValue("@Name", parameters.BreedName).Direction = ParameterDirection.Input;
command.Parameters.AddWithValue("@PetTypeId", parameters.PetTypeId).Direction = ParameterDirection.Input;
DataTable dt = SqlHelper.GetData(command);
foreach (DataRow row in dt.Rows)
{
var item = new Breed();
item.Id = Convert.ToInt32(row["Id"].ToString());
item.PetTypeId = Convert.ToInt32(row["PetTypeId"].ToString());
item.PetTypeName = (row["PetTypeName"]).ToString();
item.BreedName = row["BreedName"].ToString();
item.Description = row["Description"].ToString();
item.SortOrder = Convert.ToInt32(row["SortOrder"].ToString());
item.CreatedBy = Convert.ToInt32(row["CreatedById"].ToString());
item.CreatedByName = row["CreatedByName"].ToString();
items.Add(item);
}
return items;
}
然后我将itemMgr声明为对象。将这些项目放在对象列表中
BreedDataAccess itemMgr = new BreedDataAccess();
public void LoadGridview()
{
Breed BreedParam = RetrieveFilter(searchCrit); //gets all filter parameters for individual searching
//this.itemMgr.EntityList = itemMgr.RetrieveMany(itemMgr.Parameters);
List <Breed> listOfBreeds = new List<Breed>();
listOfBreeds = DataAcessLayer.RetrieveMany(BreedParam);
this.dgvBreeds.AutoGenerateColumns = false;
this.dgvBreeds.ItemSource = this.itemMgr.EntityList;
}
这就是我为标记做的尝试:
<Window x:Class="NeoClinic.WPFTryOut.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>
<DataGrid x:Name="dgvBreeds" HorizontalAlignment="Left" VerticalAlignment="Top" Height="319" Width="517"/>
</Grid>
我尝试使用Windows格式的DataGridView代码,它运行得很好 但我不知道如何在WPF中做到这一点。
答案 0 :(得分:1)
我看到你正在处理事件,大概是在窗口/控件后面的代码中。这在UI元素和后面的逻辑之间创建了一个强大的耦合,这反过来使测试变得更加困难。
我会考虑使用MVVM pattern并在View Model中执行此类操作。使用此方法可以减少UI(视图)和逻辑(视图模型)之间的耦合。
我已经整理了一个非常简单的应用程序(目标.Net 4.5),以演示将集合绑定到数据网格以及如何删除所选行。
这是XAML:
<Window x:Class="WpfApplication1.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow"
Width="525"
Height="350">
<Grid>
<StackPanel>
<DataGrid ItemsSource="{Binding People}" SelectedItem="{Binding SelectedItem}" />
<Button Command="{Binding DeleteCommand}">Delete Selected Row</Button>
</StackPanel>
</Grid>
</Window>
MainWindow.xaml.cs
using System;
using System.Windows;
using System.Windows.Input;
namespace WpfApplication1
{
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
DataContext = new MainViewModel();
}
}
Person.cs
using System;
namespace WpfApplication1
{
public class Person
{
public string Forename { get; set; }
public string Surname { get; set; }
}
}
DelegateCommand.cs
using System;
using System.Windows;
using System.Windows.Input;
namespace WpfApplication1
{
public class DelegateCommand : ICommand
{
private readonly Predicate<object> _canExecute;
private readonly Action<object> _execute;
public event EventHandler CanExecuteChanged;
public DelegateCommand(Action<object> execute)
: this(execute, null)
{
}
public DelegateCommand(Action<object> execute,
Predicate<object> canExecute)
{
_execute = execute;
_canExecute = canExecute;
}
public bool CanExecute(object parameter)
{
if (_canExecute == null)
{
return true;
}
return _canExecute(parameter);
}
public void Execute(object parameter)
{
_execute(parameter);
}
public void RaiseCanExecuteChanged()
{
if (CanExecuteChanged != null)
{
CanExecuteChanged(this, EventArgs.Empty);
}
}
}
}
MainViewModel.cs
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Runtime.CompilerServices;
using System.Windows.Input;
namespace WpfApplication1
{
internal class MainViewModel : INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged;
public MainViewModel()
{
People = new ObservableCollection<Person>();
DeleteCommand = new DelegateCommand(x => DeleteSelectedItem(null));
People.Add(new Person { Forename = "Bob", Surname = "Smith" });
People.Add(new Person { Forename = "Alice", Surname = "Jones" });
}
private void DeleteSelectedItem(object obj)
{
People.Remove(SelectedItem);
SelectedItem = null;
}
public ICommand DeleteCommand { get; set; }
public ObservableCollection<Person> People { get; set; }
private Person selectedItem;
public Person SelectedItem
{
get { return selectedItem; }
set
{
if (selectedItem == value)
return;
selectedItem = value;
OnPropertyChanged();
}
}
protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = null)
{
PropertyChangedEventHandler handler = PropertyChanged;
if (handler != null) handler(this, new PropertyChangedEventArgs(propertyName));
}
}
}