将ObservableCollection <t> ItemSource绑定到DataGrid

时间:2016-12-06 10:41:45

标签: c# wpf datagrid prism

在我的wpf应用程序中,我使用 Prism 库将我的viewmodel自动装入我的视图中。我有这个工作的简单属性,自动绑定视图和模型。现在我试图将ObservableCollection<T>数据绑定到DataGrid但没有运气。以下是我当前场景的结构。

ConfigurationDetails.cs

public class ConfigurationDetails:BindableBase
{
    private int _id;
    public int Id { get { return _id; } set { SetProperty(ref _id, value); } }

    private string _configFName;
    private string _configSName;
    private string _configUName;

    public string ConfigFName { get { return _configFName; } set { SetProperty(ref _configFName, value); } }
    public string ConfigSName { get { return _configSName; } set { SetProperty(ref _configSName, value); } }
    public string ConfigUName { get { return _configUName; } set { SetProperty(ref _configUName, value); } }
}

ConfigurationWindowViewModel.cs

public class ConfigurationWindowViewModel : BindableBase
{
    public ConfigurationWindowViewModel()
    {
        ConfigDetails = new ObservableCollection<ConfigurationDetails>();
    }
    private ObservableCollection<ConfigurationDetails> _configDetails;
    public ObservableCollection<ConfigurationDetails> ConfigDetails { get { return _configDetails; } set { SetProperty(ref _configDetails, value); } }
}

ConfigurationWindow.xaml

<UserControl x:Class="MyApp.Views.ConfigurationWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:prism="http://prismlibrary.com/"
        prism:ViewModelLocator.AutoWireViewModel="True"
        HorizontalContentAlignment="Center">
    ....
    <DataGrid ItemsSource="{Binding ConfigDetails}" AutoGenerateColumns="False">
         <DataGrid.Columns>
              <DataGridTextColumn Header="F NAME" Width="*" Binding="{Binding Path=ConfigFName}"/>
              <DataGridTextColumn Header="S NAME" Width="*" Binding="{Binding Path=ConfigSName}"/>
              <DataGridTextColumn Header="U NAME" Width="*" Binding="{Binding Path=ConfigUName}"/>
         </DataGrid.Columns>
    </DataGrid>
    ....
</UserControl>

ConfigurationWindow.xaml.cs

public ConfigurationWindow()
{
     InitializeComponent();
     using (_db = new MyEntities())
     {
         var configRecords = _db.tblConfigs.ToList().Select(x => new ConfigurationDetails()
         {
              ConfigFName = x.ConfigFName,
              ConfigSName = x.ConfigSName,
              ConfigUName = x.ConfigUName,
              Id = x.ConfigID
         });
         model.ConfigDetails = new ObservableCollection<ConfigurationDetails>(configRecords);
         //model.ConfigDetails will have records assigned to it when debugged
        }
     }
 }

但是,我仍然没有看到DataGrid中显示的任何记录。这里缺少什么。我还使用了ViewModelLocator.AutoWireViewModel,它一直在为其他模型属性完美地工作。希望能在这里得到一些帮助。

更新 - 我将ConfigurationWindowViewModel.cs放在ViewModels文件夹中。很抱歉没有提及。

2 个答案:

答案 0 :(得分:2)

您在哪里将ConfigurationDetails对象添加到ConfigDetails集合?尝试在ConfigurationWindowViewModel类的构造函数中添加一些项:

public class ConfigurationWindowViewModel : BindableBase
{
   public ConfigurationWindowViewModel()
   {
    ConfigDetails = new ObservableCollection<ConfigurationDetails>();
    //add some items...:
    ConfigDetails.Add(new ConfigurationDetails() { ConfigFName = "F", ConfigSName = "S", ConfigUName = "U" });
    ConfigDetails.Add(new ConfigurationDetails() { ConfigFName = "F", ConfigSName = "S", ConfigUName = "U" });
    ConfigDetails.Add(new ConfigurationDetails() { ConfigFName = "F", ConfigSName = "S", ConfigUName = "U" });
   }
   private ObservableCollection<ConfigurationDetails> _configDetails;
   public ObservableCollection<ConfigurationDetails> ConfigDetails { get { return _configDetails; } set { SetProperty(ref _configDetails, value); } }
}

要使Prism中的autowire功能能够开箱即用,您需要遵循默认的命名约定,即ConfigurationWindowViewModel类应该位于&#34; ViewModels&#34;项目根文件夹中的文件夹(以及.ViewModels命名空间)。请参阅Brian Lagunas&#39;有关此内容以及如何通过在Bootstrapper或App.xaml.cs中调用ViewModelLocationProvider.SetDefaultViewTypeToViewModelTypeResolver方法来更改默认约定的博客文章:http://brianlagunas.com/getting-started-prisms-new-viewmodellocator/

还要确保实现IView接口:

public partial class ConfigurationWindow() : Window, IView
{
...
}

答案 1 :(得分:0)

嗯......首先,ObservableCollection实现了INotifyPropertyChanged,因此您不必这样做。

然后这就够了:

public ObservableCollection<ConfigurationDetails> ConfigDetails { get; set; } 


public ConfigurationWindow()
{
   InitializeComponent();
   using (_db = new MyEntities())
   {
      var configRecords = _db.tblConfigs.ToList().Select(x => new ConfigurationDetails()
     {
          ConfigFName = x.ConfigFName,
          ConfigSName = x.ConfigSName,
          ConfigUName = x.ConfigUName,
          Id = x.ConfigID
     });
     foreach(var config in configRecords)
     { 
        model.ConfigDetails.Add(config);
      }


    }
 }

}