<CLR部分>
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

using System.ComponentModel;            //依存プロパティ、DependencyObjectに必要

namespace IT_Tutorial2_5
{
    public partial class MainWindow : Window
    {
        Person person = new Person();

        public MainWindow()
        {
            InitializeComponent();
            
            //スライダーの値をラベルのコンテンツにバインディングその1
            var binding = new Binding
            {
                ElementName = "Slider1",
                Path=new PropertyPath("Value"),
            };
            Label1.SetBinding(Label.ContentProperty, binding);

            //スライダーの値をラベルのコンテンツにバインディングその2(書き方が違うだけ)
            var binding2 = new Binding();
            binding2.ElementName="Slider2";
            binding2.Path=new PropertyPath("Value");
            Label2.SetBinding(Label.ContentProperty, binding2);

            //自作クラスをバインディング
            Binding personBinding = new Binding();
            personBinding.Source=person;
            personBinding.Path = new PropertyPath(Person.NameProperty); //パスには依存プロパティを指定する
            //personBinding.Path = new PropertyPath("Name"); //DependencyProperty.Registerでセットした文字列でも指定OK
            Label3.SetBinding(Label.ContentProperty, personBinding);
        }

        //ボタンを押す事によって値を変更し通知が行われているか確認できる
        private void OnClickA(object sender, RoutedEventArgs e)
        {
            person.Name = "山田";
        }

        private void OnClickB(object sender, RoutedEventArgs e)
        {
            person.Name = "岸辺";
        }
    }

    //DependencyObjectを継承するとINotifyPropertyChanged系の実装が暗黙自動的に行われている
    class Person : DependencyObject
    {
        //依存プロパティを生成
        public static readonly DependencyProperty NameProperty = DependencyProperty.Register("Name", typeof(string), typeof(Person));
        public static readonly DependencyProperty AddresProperty = DependencyProperty.Register("Addres", typeof(string), typeof(Person));

        public string Name
        {
            get { return (string)GetValue(NameProperty); }  //GetValueにより読み取りも通知される
            set { SetValue(NameProperty, value); }          //値の変更はSetValueによって行う
        }

        public string Addres
        {
            get { return (string)GetValue(AddresProperty); }
            set { SetValue(AddresProperty, value); }
        }

        public int ID { get { return ID; } }

        public Person() { }       //WPFでXAMLで利用する場合、空のctorが必要
    }
}

<XAML部分>
<Window x:Class="IT_Tutorial2_5.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="350" Width="525">
    <StackPanel>
        <Slider Name="Slider1" />
        <Label Name="Label1"/>
        <Slider Name="Slider2" />
        <Label Name="Label2"/>
        <Button  Width="50" Height="50" Name="Button1" Content="A" Click="OnClickA"/>
        <Button  Width="50" Height="50" Name="Button2" Content="B" Click="OnClickB"/>
        <Label Name="Label3"/>
    </StackPanel>
</Window>
最終更新:2012年10月13日 02:21