1

该程序运行良好,不会崩溃或其他什么。但是数据没有显示在表格上(datagrid)

更新后的版本:

查看: Userperspective.xaml

我在 xaml 文件中遇到错误,因为绑定路径“产品”是未知的数据上下文

  <Grid Margin="0,0,0,-20">
        <DataGrid Name="Producttable" ItemsSource="{Binding Path=Products}"                 
                  HorizontalAlignment="Left" Height="200" Margin="10,44,0,0"  
                  VerticalAlignment="Top" Width="972"  />

查看: Userperspective.xaml.cs

public partial class Userperspective : Window
{
    public Userperspective()
    {
        InitializeComponent();
        DataContext = new ProductViewModel();
    }
}

产品视图模型

      private readonly Product _product;
    private IBackend _backend;
    public ICommand ProductCommand { get; set; }
    public IList<Product> Products { get; set; }

    public ProductViewModel()
    {
        _backend = new BackendService();
        _product = new Product();
        ProductCommand = new ProductCommand(this);
    }

    public Product Product()
    {
        return _product;
    }

    public void LoadProducts()
    {
        Products = _backend.GetProducts();
        RaisePropertyChanged("Products");
    }

产品指令

    private readonly ProductViewModel _vm;

    public ProductCommand(ProductViewModel vm)
    {
        this._vm = vm;
    }      

    public void Execute(object parameter)
    {
        _vm.LoadProducts();
    }

后端服务

namespace _blabla
{
    class BackendService : IBackend
    {
        public IList<Product> GetProducts()
        {
            using (var db = new NORTHWNDEntities())
            {
                var query = from p in db.Products
                            select new Product
                            {
                                Name = p.ProductName,
                            };

                return query.ToList();
            }
        }
    }
}

后端

namespace _blabla.Commands
{
    public interface IBackend
    {
        IList<Product> GetProducts();    
    }
}
4

2 回答 2

1

鉴于您是 WPF 和 MVVM 的新手,您应该将问题分解为更易于管理的问题。您的代码中发生了很多事情;MVVM、命令、数据库访问和一些抽象。你的意图是正确的,但它并不容易解决这个问题。

根据您提供的信息,我什至不能 100% 确定问题出在哪里,但我怀疑它要么是绑定,要么是数据库访问。我将专注于向您展示绑定方面。

看到我无权访问您的数据库代码,我已经模拟了一些类来帮助我解决这个问题。

注意:命令代码是噪音,所以我将从我的答案中删除它并专注于绑定到产品列表(一旦它工作,你可以将它与你的命令解决方案集成)。

产品

public class Product
{
    public string Name { get; set; }
    public string Description { get; set; }

    public override string ToString()
    {
        return string.Format("Product: ({0}), {1}", Name, Description);
    }
}

BackendService:这基本上返回一个产品数组,而不是能够访问数据库。

class BackendService : IBackend
{
    public IList<Product> GetProducts()
    {
        return new Product[]
        {
            new Product{ Name = "Laptop", Description = "Dell 17inch laptop" },
            new Product{ Name = "Mobile Phone", Description = "iPhone" },
            new Product{ Name = "Television", Description = "Samsung 32 inch plasma" },
            new Product{ Name = "Car", Description = "Gran Torino" },
            new Product{ Name = "Book", Description = "Effective C#" },
        };
    }
}

我已将 viewModel 中的产品列表绑定到 a Listbox,因为我无权访问,DataGrid否则我没有修改主窗口代码。

主窗口.xaml

<Grid>
    <Grid.RowDefinitions>
        <RowDefinition Height="*" />
        <RowDefinition Height="Auto" />
    </Grid.RowDefinitions>
    <ListBox Margin="5"
             ItemsSource="{Binding Path=GetProducts}"/>
</Grid>

主窗口.cs

public partial class MainWindow : Window
{
    public MainWindow()
    {
        InitializeComponent();
        DataContext = new ProductViewModel();
    }
}

现在如果我使用你的 viewModel,我会得到一个NullReferenceException源自你的调用的,_backend.GetProducts()因为你没有实例化你的BackendService. 如果我像这样更新构造函数:

public ProductViewModel()
{
    _backend = new BackendService();
    _product = new Product();
    ProductCommand = new ProductCommand(this);
}

并运行应用程序,产品列表正确显示。

在此处输入图像描述

您应该能够将我提供的代码集成到您的项目中并证明它正在工作。当您对此感到满意时,您应该更新BackendService该类以调用数据库中的产品列表。我建议您理所当然地对所有绑定执行此操作,这样您就知道是绑定不起作用还是数据库调用。

于 2012-12-20T20:22:58.273 回答
0

您正在尝试执行GetProducts,但这是一个属性而不是方法 - 创建一个单独的方法来加载产品并将您的属性名称更改为更有意义的名称

public IList<Product> Products {get;set;}

然后创建一种方法来加载您的产品

public void LoadProducts()
{
    Products = _backend.GetProducts();
    //You will need to notify of property change here
    OnPropertyChanged("Products");
}

然后绑定到Products你的 xaml

    <Window x:Class="_blabla.View.Userperspective"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="UserPerspective" Height="500" Width="1000">

    <Grid Margin="0,0,0,-20">
        <DataGrid Name="Producttable" ItemsSource="{Binding Path=Products}"                 
                  HorizontalAlignment="Left" Height="200" Margin="10,44,0,0"  
                  VerticalAlignment="Top" Width="972"  />
    </Grid>
</Window>

然后在你的命令调用中LoadProducts

public void Execute(object parameter)
{
    _vm.LoadProducts();
}

您将需要实现INotifyPropertyChanged以便 UI 知道您已更改Products属性

于 2012-12-20T16:16:38.990 回答