我有一個類「Movie_Class」與來自xml文件的數據相關。 讀書,我使用一個類「StoreDbDataSet」的xml文件:如何迭代通過逗號分隔的字符串屬性,拆分字符串,並從所有不同的值創建一個ICollection
public class StoreDbDataSet
{
public DataTable GetMovies()
{
return ReadDataSet().Tables[0];
}
internal static DataSet ReadDataSet()
{
DataSet ds = new DataSet();
ds.ReadXml("Peter_Movie_Database.xml");
return ds;
}
}
的「Movie_Class」:
public class Movie_Class : INotifyPropertyChanged
{
private string mediaLabel;
public string MediaLabel
{
get { return mediaLabel; }
set
{
mediaLabel = value;
OnPropertyChanged(new PropertyChangedEventArgs("MediaLabel"));
}
}
private string year;
public string Year
{
get { return year; }
set
{
year = value;
OnPropertyChanged(new PropertyChangedEventArgs("Year"));
}
}
private string originalTitle;
public string OriginalTitle
{
get { return originalTitle; }
set
{
originalTitle = value;
OnPropertyChanged(new PropertyChangedEventArgs("OriginalTitle"));
}
}
private string director;
public string Director
{
get { return director; }
set
{
director = value;
OnPropertyChanged(new PropertyChangedEventArgs("Director"));
}
}
private string category;
public string Category
{
get { return category; }
set
{
category = value;
OnPropertyChanged(new PropertyChangedEventArgs("Category"));
}
}
public Movie_Class(string originalTitle, string year,
string director, string mediaLabel, string category)
{
OriginalTitle = originalTitle;
Year = year;
Director = director;
MediaLabel = mediaLabel;
Category = category;
}
public event PropertyChangedEventHandler PropertyChanged;
public void OnPropertyChanged(PropertyChangedEventArgs e)
{
if (PropertyChanged != null)
PropertyChanged(this, e);
}
}
我創建的所有獨特的「年」性質的ICollection的。請參閱下面的代碼中的GetDistinctYear()。然後,我可以將此Icollection用作wpf中組合框的ItemsSource。這工作正常。 但是現在我想爲一個名爲「Category」的屬性做同樣的事情。問題是這是一個逗號分隔的字符串。 (例如:戲劇,驚悚片,戰爭) 我不知道如何遍歷所有「類別」屬性,拆分字符串並使Icollection包含所有不同的類別。
public class StoreDb
{
public ICollection<Movie_Class> GetMovies()
{
DataSet ds = StoreDbDataSet.ReadDataSet();
ObservableCollection<Movie_Class> movies = new ObservableCollection<Movie_Class>();
foreach (DataRow productRow in ds.Tables["Movie"].Rows)
{
movies.Add(new Movie_Class((string)productRow["OriginalTitle"],
(string)productRow["Year"], (string)productRow["Director"],
(string)productRow["MediaLabel"], (string)productRow["Category"]));
}
return movies;
}
public ICollection<Movie_Class> GetDistinctYear()
{
ICollection<Movie_Class> movies = GetMovies();
IEnumerable<Movie_Class> matches = from movie in movies
group movie by movie.Year into grps
select grps.First();
return new ObservableCollection<Movie_Class>(matches.ToList());
}
public ICollection<Movie_Class> GetDistinctCategory()
{
}
}
從我的.cs的mainwindow.xaml的文件中的代碼:
public partial class MainWindow : Window
{
private ICollection<Movie_Class> moviesByYear;
private ICollection<Movie_Class> moviesByCategory;
private ICollection<Movie_Class> AllMovies;
public MainWindow()
{
InitializeComponent();
AllMovies = App.StoreDb.GetMovies();
moviesByYear = App.StoreDb.GetDistinctYear();
ComboBox_Year.ItemsSource = moviesByYear;
moviesByCategory = App.StoreDb.GetDistinctCategory();
ComboBox_Category.ItemsSource = moviesByCategory;
ICollectionView view = CollectionViewSource.GetDefaultView(ComboBox_Year.ItemsSource);
view.SortDescriptions.Add(new SortDescription("Year", ListSortDirection.Ascending));
}
解決方案使用「公共的ICollection」。如果我想用這個作爲組合框的ItemsSource,那麼我得到一個錯誤。我使用這個代碼: 'AllMovies = App.StoreDb.GetMovies();在我的.cs文件的mainwindow.xaml我使用下面的代碼: 'AllMovies = App.StoreDb.GetMovies(); moviesByCategory = App.StoreDb.GetDistinctCategory(); ComboBox_Category.ItemsSource = moviesByCategory; ' 錯誤: 「無法將類型'System.Collections.Generic.ICollection '隱式轉換爲'System.Collections.Generic.ICollection '。存在明確的轉換(您是否缺少演員?)「 –
您應該將」moviesByCategory「字段的類型從ICollection更改爲ICollection 。然後編譯。 –
mm8
或者你可以刪除該字段,只是:'ComboBox_Category.ItemsSource = App.StoreDb.GetDistinctCategory();' – mm8