WPF - バインド基礎


WPFバインド

シンプルなバインドを確認。日本語の書籍が少ないためMSDNは必読です。

Windows1.xml
<Window x:Class="WpfApplication1.Window1"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="Window1" Height="131" Width="300">
    <Grid>
        <TextBox Name="textBox1" Text="{Binding Path=Point,UpdateSourceTrigger=PropertyChanged}" Height="26" VerticalAlignment="Top" Margin="12,12,12,0" Background="Tan"></TextBox>
        <Button Name="button1" Click="button1_Click" HorizontalAlignment="Left" Width="73" Margin="65,44,0,0" Height="41" VerticalAlignment="Top">MsgBox</Button>
        <Button Margin="0,44,61,0" Name="button2" Click="button2_Click" HorizontalAlignment="Right" Width="73" Height="41" VerticalAlignment="Top"></Button>
    </Grid>
</Window>
Windows1.xml.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
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;

namespace WpfApplication1
{
    /// <summary>
    /// Window1.xaml の相互作用ロジック
    /// </summary>
    public partial class Window1 : Window
    {
        TestData testdata;

        public Window1()
        {
            InitializeComponent();
            this.testdata = new TestData("きいろ");
            this.DataContext = this.testdata;
        }

        private void button1_Click(object sender, RoutedEventArgs e)
        {
            MessageBox.Show(this.testdata.Point);
        }

        private void button2_Click(object sender, RoutedEventArgs e)
        {
            this.testdata.Point = "あお";
        }
    }
}
|<

**TestData.cs
>|
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Media;
using System.ComponentModel;

namespace WpfApplication1
{
    public class TestData : INotifyPropertyChanged
    {
        public event PropertyChangedEventHandler PropertyChanged;

        private string point;

        public string Point
        {
            get { return this.point; }
            set
            {
                this.point = value;
                this.NotifyChanged("Point");
            }
        }

        public TestData(string point)
        {
            this.point = point;
        }

        void NotifyChanged(string property)
        {
            if (PropertyChanged != null)
            {
                PropertyChanged(this, new PropertyChangedEventArgs(property));
            }
        }
    }
}

エッセンシャルWPF:Windows Presentation Foundation (Programmer's SELECTION)

エッセンシャルWPF:Windows Presentation Foundation (Programmer's SELECTION)

XAMLプログラミング WPFアプリケーションの概要と開発

XAMLプログラミング WPFアプリケーションの概要と開発