To bind MultiSelect to a data source, follow these steps:
C# |
コードのコピー
|
---|---|
using System; using System.IO; using System.Reflection; using System.Collections.Generic; using System.Xml.Serialization; using Windows.Storage; using System.Threading.Tasks; using System.ComponentModel.DataAnnotations; using System.Collections.ObjectModel; namespace MultiSelect_UWP { public class NorthwindData { [Display(Name = "CustomerID")] public string CustomerID { get; set; } [Display(Name = "CompanyName")] public string CompanyName { get; set; } [Display(Name = "ContactName")] public string ContactName { get; set; } [Display(Name = "ContactTitle")] public string ContactTitle { get; set; } [Display(Name = "Address")] public string Address { get; set; } [Display(Name = "City")] public string City { get; set; } [Display(Name = "PostalCode")] public string PostalCode { get; set; } [Display(Name = "Country")] public string Country { get; set; } [Display(Name = "Phone")] public string Phone { get; set; } [Display(Name = "Fax")] public string Fax { get; set; } } public class NorthwindStorage { public static async Task<ObservableCollection<NorthwindData>> Load() { try { Uri resourceUri = new Uri("ms-appx:///Resources/Northwind.xml"); var file = await StorageFile.GetFileFromApplicationUriAsync(resourceUri); var fileStream = await file.OpenAsync(FileAccessMode.Read); var xmls = new XmlSerializer(typeof(ObservableCollection<NorthwindData>)); return (ObservableCollection<NorthwindData>)xmls.Deserialize(fileStream.AsStream()); } catch (Exception e) { throw new FileNotFoundException("File not found"); } } } } |
C# |
コードのコピー
|
---|---|
Dispatcher.RunAsync(CoreDispatcherPriority.Normal,
new DispatchedHandler(PopulateUnboundGrid));
|
Add the following code to fill MultiSelect with data.
C# |
コードのコピー
|
---|---|
private async void PopulateUnboundGrid() { var data = await NorthwindStorage.Load(); c1MultiSelect1.ItemsSource = data; c1MultiSelect1.DisplayMemberPath = "CustomerID"; } |