ソース掲示板




すべてから検索

キーワード   条件 表示 現行ログ 過去ログ トピックス 名前 本文
Xaml 画面にデータをバインドするクラスのテンプレート
日時: 2013/09/28 04:05
名前: lightbox



データ バインディングの概要

拡張子:
<!-- 一覧的なリストボックス -->
<ListBox
	x:Name="MainListBox"
	Margin="564,34,236,229"
	ItemsSource="{Binding Items}" Grid.Row="1">
	<ListBox.ItemTemplate>
		<DataTemplate>
			<StackPanel Margin="0,0,0,17" Width="432" Height="78">
				<TextBlock
					Text="{Binding LineOne}"
					TextWrapping="Wrap"/>
				<TextBlock
					Text="{Binding LineTwo}"
					TextWrapping="Wrap"
					Margin="12,-6,12,0" FontSize="24" />
			</StackPanel>
		</DataTemplate>
	</ListBox.ItemTemplate>
</ListBox>
拡張子:
MainViewModel mvm = JsonConvert.DeserializeObject<ItemViewModel>(JsonText);
MainListBox.DataContext = mvm;

拡張子:
using System;
using System.ComponentModel;
using System.Collections.Generic;
using System.Diagnostics;
using System.Text;
using System.Windows;
using System.Windows.Input;
using System.Collections.ObjectModel;


namespace BindingXaml {

	public class MainViewModel : INotifyPropertyChanged {
		public MainViewModel() 	{
			// バインド用のコレクションのインスタンスを設定
			this.Items = new ObservableCollection<ItemViewModel>();
		}

		// *****************************************************
		// バインド用のコレクションのプロパティ
		// *****************************************************
		public ObservableCollection<ItemViewModel> Items { get; private set; }

		// *****************************************************
		// データが変更された事を通知する為の実装
		// *****************************************************
		public event PropertyChangedEventHandler PropertyChanged;
		private void NotifyPropertyChanged(String propertyName) {
			PropertyChangedEventHandler handler = PropertyChanged;
			if (null != handler) {
				handler(this, new PropertyChangedEventArgs(propertyName));
			}
		}
	}
}
メンテナンス

ItemViewModel.cs ( No.1 )
日時: 2013/09/28 03:47
名前: lightbox


日時: 2013/09/28 03:47
名前: lightbox
拡張子:
using System;
using System.ComponentModel;
using System.Diagnostics;
using System.Linq.Expressions;
using System.Net;
using System.Windows;
using System.Windows.Input;

namespace BindingXaml { 

	public class ItemViewModel : INotifyPropertyChanged 	{
		// *********************************************
		// エントリ
		// *********************************************
		private string _lineOne;
		public string LineOne 	{
			get { return _lineOne; }
			set {
				SetAndNotifyString(GetName(() => LineOne), ref _lineOne, value);
			}
		}

		// *********************************************
		// エントリ
		// *********************************************
		private string _lineTwo;
		public string LineTwo 	{
			get { return _lineTwo; }
			set {
				SetAndNotifyString(GetName(() => LineTwo), ref _lineTwo, value);
			}
		}

		// *********************************************
		// エントリ
		// *********************************************
		private string _lineThree;
		public string LineThree
		{
			get { return _lineThree; }
			set {
				SetAndNotifyString(GetName(() => LineThree), ref _lineThree, value);
			}
		}

		// *********************************************
		// 文字列プロパティ用のセットメソッド
		// *********************************************
		private void SetAndNotifyString(string PropName, ref string OldData, string NewData) {
			// 無駄の無いように、値が違った時だけ処理
			if (OldData != NewData) {
				// 値の変更
				OldData = NewData;
				// 値が変更された事をバインドシステムに通知
				NotifyPropertyChanged(PropName);
			}
		}

		// *********************************************
		// プロパティを文字列に変換するメソッド
		// *********************************************
		private string GetName<T>(Expression<Func<T>> e)
		{
			var member = (MemberExpression)e.Body;
			return member.Member.Name;
		}

		// *****************************************************
		// データが変更された事を通知する為の実装
		// *****************************************************
		public event PropertyChangedEventHandler PropertyChanged;
		private void NotifyPropertyChanged(String propertyName) {
			PropertyChangedEventHandler handler = PropertyChanged;
			if (null != handler) {
				handler(this, new PropertyChangedEventArgs(propertyName));
			}
		}
	}
}
このアーティクルの参照用URLをクリップボードにコピー メンテナンス