简体   繁体   English

WPF将列表绑定到DataGrid

[英]WPF Binding a List to a DataGrid

This is my first time working with a WPF datagrid. 这是我第一次使用WPF数据网格。 From what I understand I am supposed to bind the grid to a public propery in my viewmodel. 根据我的理解,我应该将网格绑定到我的viewmodel中的公共属性。 Below is the ViewModel code, as I step through the debugger GridInventory is getting set to List containing 2606 records however these records never show in the datagrid. 下面是ViewModel代码,因为我逐步调试GridInventory被设置为包含2606条记录的List,但这些记录从未在datagrid中显示。 What am I doing wrong? 我究竟做错了什么?

public class ShellViewModel : PropertyChangedBase, IShell
{
    private List<ComputerRecord> _gridInventory;

    public List<ComputerRecord> GridInventory
    {
        get { return _gridInventory; }
        set { _gridInventory = value; }
    }

    public void Select()
    {
        var builder = new SqlConnectionBuilder();
        using (var db = new DataContext(builder.GetConnectionObject(_serverName, _dbName)))
        {
            var record = db.GetTable<ComputerRecord>().OrderBy(r => r.ComputerName);                
            GridInventory = record.ToList();
        }
    }
}

My XAML is 我的XAML是

<Window x:Class="Viewer.Views.ShellView"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="InventoryViewer" Height="647" Width="1032" WindowStartupLocation="CenterScreen">
<Grid>
    <DataGrid x:Name="GridInventory" ItemsSource="{Binding GridInventory}"></DataGrid>
    <Button x:Name="Select" Content="Select" Height="40" Margin="600,530,0,0" Width="100" />
</Grid>
</Window>

i think you need call raisepropertychanged event in your GridInventory setter so that view can get notified. 我认为您需要在GridInventory setter中调用raisepropertychanged事件,以便可以通知视图。

public List<ComputerRecord> GridInventory
{
    get { return _gridInventory; }
    set 
    { _gridInventory = value; 
      RaisePropertyChanged("GridInventory");
    }
}

The page's datacontext is not bound to an instance of the View Model. 页面的datacontext未绑定到View Model的实例。 In the code behind after the InitializeComponent call, assign the datacontext such as: 在InitializeComponent调用之后的代码中,分配datacontext,例如:

InitializeComponent();

DataContext = new ShellViewModel();

I think you should use the RaisePropertyChanged in the ViewModel and Model and also should set the DataContext in the View. 我认为您应该在ViewModel和Model中使用RaisePropertyChanged,还应该在View中设置DataContext。

<Window.DataContext>    
    <local:ShellViewModel />    
</Window.DataContext>

You might want to consider using bind ObservableCollection to the datagrid. 您可能需要考虑将bind ObservableCollection用于datagrid。 Then you don't need to maintain a private member _gridInventory and a public property GridInventory 然后,您不需要维护私有成员_gridInventory和公共属性GridInventory

//viewModel.cs
public ObservableCollection<ComputerRecord> GridInventory {get; private set;}
//view.xaml
<DataGrid ... ItemsSource="{Binding GridInventory}" .../>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM