メモ帳ブログ @ wiki
バインディング
最終更新:
nina_a
-
view
バインディング
例(エレメントのプロパティ同士)
- <Slider Height="33" HorizontalAlignment="Center" Width="400"
- Minimum="10" Maximum="100" Interval="1" Name="fslider"/>
- <TextBlock HorizontalAlignment="Center" Margin="0,20,0,0"
- FontSize="{Binding ElementName=fslider, Path=Value}">
- こんにちは
- </TextBlock>
プログラムの場合は以下.面倒なので必要がなければXAMLでやる.
- // XAML
- <Slider Height="33" HorizontalAlignment="Center" Width="400"
- Minimum="10" Maximum="100" Name="fslider"/>
- <TextBlock HorizontalAlignment="Center" Margin="0,20,0,0" Name="ftext">
- こんにちは
- </TextBlock>
- // C#
- Binding b = new Binding();
- b.Source = fslider;
- b.Path = new PropertyPath("Value");
- b.Mode = BindingMode.TwoWay;
- ftext.SetBinding(TextBlock.FontSizeProperty, b);
例(エレメントとオブジェクトのバインディング)
Valueというプロパティを持つNumber型をバインディングしてみる.
これがNumberクラス
これがNumberクラス
- using System;
- using System.Collections.Generic;
- 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.Shapes;
- using System.ComponentModel;
-
- namespace Test
- {
- public class Number
- : INotifyPropertyChanged
- {
- public Number()
- {
- Value = 0;
- }
-
- public event PropertyChangedEventHandler PropertyChanged;
-
- private void NotifyPropertyChanged(String info)
- {
- if (PropertyChanged != null)
- PropertyChanged(this, new PropertyChangedEventArgs(info));
- }
-
- // Valueプロパティ
- private Int32 _Value;
- public Int32 Value
- {
- get { return _Value; }
- set {
- _Value = value;
- NotifyPropertyChanged("Value");
- }
- }
- }
- }
Int32をStringに変換するIValueConverter
- using System;
- using System.Collections.Generic;
- 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.Shapes;
- using System.Globalization;
-
- namespace Test
- {
- [ValueConversion(typeof(Int32), typeof(String))]
- public class NumberConverter
- : IValueConverter
- {
- public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
- {
- return ((Int32) value).ToString();
- }
-
- public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
- {
- throw new NotImplementedException();
- }
- }
- }
そしてXAML.
- <Window
- xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
- xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
- xmlns:sys="clr-namespace:System;assembly=mscorlib"
- xmlns:local="clr-namespace:Test"
- x:Class="Test.MainWindow"
- x:Name="Window"
- Title="MainWindow"
- Width="640" Height="480">
-
- <Window.Resources>
- <local:Number x:Key="number"/>
- <local:NumberConverter x:Key="nc"/>
- </Window.Resources>
-
- <StackPanel>
- <TextBlock HorizontalAlignment="Center" Margin="0,20,0,0" Text="{Binding Source={StaticResource number}, Path=Value, Converter={StaticResource nc}}"/>
- <StackPanel Orientation="Horizontal" HorizontalAlignment="Center">
- <Button Content="Add 10" Margin="10" Name="add"/>
- <Button Content="Subtract 10" Margin="10" Name="sub"/>
- </StackPanel>
- </StackPanel>
- </Window>
XAML.csはこんな感じ.
- public partial class MainWindow : Window
- {
- public MainWindow()
- {
- this.InitializeComponent();
-
- Number num = (Number) this.FindResource("number");
-
- this.add.Click += (e,s) => num.Value += 10;
- this.sub.Click += (e,s) => num.Value -= 10;
- }
- }
カテゴリ:WPF
