2016-12-06 115 views
1

在我的wpf應用程序中,我使用Prism庫將我的viewmodel自動裝入我的視圖中。我有這個工作自動綁定視圖和模型的簡單屬性。現在我試圖綁定一個ObservableCollection<T>數據綁定到DataGrid沒有運氣。以下是我目前場景的結構。Binding ObservableCollection <T> ItemSource to 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文件夾。對不起,提到。

+0

貴'ConfigurationWindow'實現[IVIEW(http://brianlagunas.com/getting-started-prisms-new-viewmodellocator/)? – slugster

+0

@slugster只有我們的'ViewModel'被放置在我認識的'Views'存在的相同文件夾中時才需要'IView'。否則,如果我們把它放在'ViewModels'文件夾中,'prism'會照顧對嗎? –

+0

那麼你的代碼中的模型來自哪裏(是viewmodel)?順便說一句,爲什麼你在代碼背後得到你的數據? – adminSoftDK

回答

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); } } 
} 

以及在棱鏡的自動裝配功能,工作外的開箱你需要遵循默認的命名約定,即你的ConfigurationWindowViewModel類應該位於項目根文件夾中的「ViewModels」文件夾(和.ViewModels名稱空間中)中。請參考布萊恩拉古納斯的博客文章,瞭解更多有關這一點,你怎麼可能通過調用ViewModelLocationProvider.SetDefaultViewTypeToViewModelTypeResolver方法在你的引導程序或App.xaml.cs更改默認的約定:http://brianlagunas.com/getting-started-prisms-new-viewmodellocator/

另外,還要確保你實現IVIEW接口:

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

感謝提示的朋友。我對這個知識得到了完全搞砸了我被灌數據到'model'的方式。我應該在'ViewModel'中完成它,而不是支持。上面的例子給出了一個提示,現在它的工作。 Bdw,我已將ViewModel放置在ViewModels目錄中,留下棱鏡來照顧Autowiring的東西。再次感謝。 –

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); 
     } 


    } 
} 

}

+0

對不起,但這不是問題..謝謝你的努力,雖然.. –

相關問題