Silverlight/WPFでデータバインディングを利用しOutlookライクなスケジュールを構築する その1
Silverlight編
Silverlight/WPFでOutlookライクなアプリ構築(1)
ダウンロード サンプルファイル (1022.7 KB)
-
- Page 6
画面に表示させるデータをコードで生成する
今回はコード ビハインドを用いて画面に表示するデータを生成します。まず、先ほど作成した ScheduleDataViewModel プロジェクトを xamSchedule_SL プロジェクトに追加します。追加する場合は、プロジェクトの [参照設定] のコンテキスト メニューから [参照の追加] を選択します。その後表示される追加ダイアログで [プロジェクト] タブを選択し、ScheduleDataViewModel を追加します。
次に xamSchedule_SL プロジェクトに新しいフォルダーを "ViewModels" と名付け作成し、
そのフォルダー内に新しいクラスを "MainPageViewModel.cs" と名付け、作成します。
MainPageViewModel.cs クラスでは、先ほど参照を追加した ScheduleDataViewModel を用いて、Resource, ResourceCalendar, Appointment を格納するプロパティを作成します。また、現在のユーザーを示す、CurrentUserId プロパティを実装します。
using System;
using ScheduleDataViewModel;
using System.Collections.ObjectModel;
namespace xamSchedule_SL.ViewModels
{
public class MainPageViewModel : ViewModelBase
{
#region プロパティ
private ObservableCollection<ResourceInfo> _resources;
public ObservableCollection<ResourceInfo> Resources
{
get { return _resources; }
set
{
_resources = value;
OnPropertyChanged("Resources");
}
}
private ObservableCollection<ResourceCalendarInfo> _resourceCalendars;
public ObservableCollection<ResourceCalendarInfo> ResourceCalendars
{
get { return _resourceCalendars; }
set
{
_resourceCalendars = value;
OnPropertyChanged("ResourceCalendars");
}
}
private ObservableCollection<AppointmentInfo> _appointments;
public ObservableCollection<AppointmentInfo> Appointments
{
get { return _appointments; }
set
{
_appointments = value;
OnPropertyChanged("Appointments");
}
}
private string _currentUserId;
public string CurrentUserId
{
get { return _currentUserId; }
set
{
_currentUserId = value;
OnPropertyChanged("CurrentUserId");
}
}
#endregion
// その他の実装
}
}
次にコンストラクタを実装します。今回の記事ではコードでデータを作成します。ここでの注意点としては、予定の開始時刻、終了時刻は世界標準時であるということです。
using System;
using ScheduleDataViewModel;
using System.Collections.ObjectModel;
namespace xamSchedule_SL.ViewModels
{
public class MainPageViewModel : ViewModelBase
{
#region プロパティ
// 省略
#endregion
#region コンストラクタ
public MainPageViewModel()
{
// リソースを作成
Resources = new ObservableCollection<ResourceInfo>();
Resources.Add(new ResourceInfo()
{
Id = "R0001",
PrimaryResourceCalendarId = "C0001"
});
// リソースカレンダーを作成
ResourceCalendars = new ObservableCollection<ResourceCalendarInfo>();
ResourceCalendars.Add(new ResourceCalendarInfo()
{
Id = "C0001",
OwningResourceId = "R0001"
});
// 予定を追加
Appointments = new ObservableCollection<AppointmentInfo>();
Appointments.Add(new AppointmentInfo()
{
Id = "A0001",
OwningCalendarId = "C0001",
OwningResourceId = "R0001",
Start = DateTime.Now.ToUniversalTime(),
End = DateTime.Now.AddHours(2).ToUniversalTime(),
});
// 現在のユーザー ID を指定
this.CurrentUserId = this.Resources[0].Id;
}
#endregion
}
}
この MainPageViewModel は実行時に MainPage の DataContext に格納します。そのため、MainPage.xaml.cs を開き、コンストラクタで MainPageViewModel のインスタンスを作成します。
using System.Windows.Controls;
using xamSchedule_SL.ViewModels;
namespace xamSchedule_SL
{
public partial class MainPage : UserControl
{
public MainPage()
{
InitializeComponent();
MainPageViewModel vm = new MainPageViewModel();
this.DataContext = vm;
}
}
}
これでスケジュールに表示させるためのデータの作成が完了しました。
この記事は参考になりましたか?
- Infragistics NetAdvantageチュートリアル連載記事一覧
- この記事の著者
-
インフラジスティックス・ジャパン株式会社 池原 大然(イケハラ ダイゼン)
国内ベンチャー企業にて.NETエンジニアとして開発に従事、2007年インフラジスティックス・ジャパンに入社。現在デベロッパー エバンジェリストとして、.NETやWPF/Silverlight製品や技術の啓蒙活動を行う。Microsoft MVP for Client App Dev 2010/04 ...
※プロフィールは、執筆時点、または直近の記事の寄稿時点での内容です
【AD】本記事の内容は記事掲載開始時点のものです 企画・制作 株式会社翔泳社
この記事は参考になりましたか?
この記事をシェア


