More Related Content
Similar to データバインディング徹底攻略
Similar to データバインディング徹底攻略 (20)
More from Hiroyuki Mori (16)
データバインディング徹底攻略
- 2. 自己紹介
• 森 博之(もり ひろゆき)
• MS開発技術を専門としたフリーランスエンジニア
• Microsoft MVP for Visual C#
• 極東IT-Engineers 代表
• ブログ:もり ひろゆきの日々是勉強
• http://moriblog.kit-eng.com
• Twitter
• Windows8対応[基本+実用]
Windowsストアアプリ開発入門 好評発売中!
#1めとべや東京
- 12. 依存関係プロパティの実装例
public class MyText : DependencyObject
{
public static readonly DependencyProeprty TextProperty =
DependencyProperty.Register(“Text”, typeof(string),
typeof(MyText));
public string Text
{
get { return (string) this.GetValue(TextProperty); }
set { this.SetValue(TextProperty, value); }
}
}
<MyText Text=“テキスト” />
#1めとべや東京
- 14. INotifyPropertyChanged
INotifyCollectionChanged
• INotifyPropertyChanged
public class Person : INotifyPropertyChanged {
public event PropertyChangedEventHandler PropertyChanged;
private string name;
public string PersonName {
get { return name; }
set {
name = value;
OnPropertyChanged("PersonName");
}
}
protected void OnPropertyChanged(string propertyName) {
if (PropertyChanged != null) {
PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
}
}
}
#1めとべや東京
- 17. マークアップ拡張の作成
<TextBlock Text=“{demo:Hello}” />
using System;
using System.Windows.Markup;
namespace BindingDemo
{
public class HelloExtension : MarkupExtension
{
public override object ProvideValue(IServiceProvider serviceProvider)
{
return "Hello";
}
}
}
#1
今回は仕組みを理解してもらうために作
成方法を説明しましたが、マークアップ
拡張の自作は可読性を落とす原因となり
えるため注意が必要です。
だいたいの場合、データバインディング
で解決できるはずですので、まずはデー
タバインディングで解決できないか検討
しましょう。
めとべや東京
- 25. バインディングソース
public class Person {
public string PersonName { get; set; }
public string Address { get; set; }
}
<TextBlock Text=“{Binding PersonName}” />
<TextBlock Text=“{Binding Address}” />
#1めとべや東京