アットウィキロゴ
メモ帳ブログ @ wiki
掲示板 掲示板 ページ検索 ページ検索 メニュー メニュー

メモ帳ブログ @ wiki

バインディング

最終更新:

nina_a

- view
管理者のみ編集可

バインディング


例(エレメントのプロパティ同士)

  1. <Slider Height="33" HorizontalAlignment="Center" Width="400"
  2. Minimum="10" Maximum="100" Interval="1" Name="fslider"/>
  3. <TextBlock HorizontalAlignment="Center" Margin="0,20,0,0"
  4. FontSize="{Binding ElementName=fslider, Path=Value}">
  5. こんにちは
  6. </TextBlock>

プログラムの場合は以下.面倒なので必要がなければXAMLでやる.
  1. // XAML
  2. <Slider Height="33" HorizontalAlignment="Center" Width="400"
  3. Minimum="10" Maximum="100" Name="fslider"/>
  4. <TextBlock HorizontalAlignment="Center" Margin="0,20,0,0" Name="ftext">
  5. こんにちは
  6. </TextBlock>
  1. // C#
  2. Binding b = new Binding();
  3. b.Source = fslider;
  4. b.Path = new PropertyPath("Value");
  5. b.Mode = BindingMode.TwoWay;
  6. ftext.SetBinding(TextBlock.FontSizeProperty, b);

例(エレメントとオブジェクトのバインディング)

Valueというプロパティを持つNumber型をバインディングしてみる.
これがNumberクラス
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Text;
  4. using System.Windows;
  5. using System.Windows.Controls;
  6. using System.Windows.Data;
  7. using System.Windows.Documents;
  8. using System.Windows.Input;
  9. using System.Windows.Media;
  10. using System.Windows.Media.Imaging;
  11. using System.Windows.Shapes;
  12. using System.ComponentModel;
  13.  
  14. namespace Test
  15. {
  16. public class Number
  17. : INotifyPropertyChanged
  18. {
  19. public Number()
  20. {
  21. Value = 0;
  22. }
  23.  
  24. public event PropertyChangedEventHandler PropertyChanged;
  25.  
  26. private void NotifyPropertyChanged(String info)
  27. {
  28. if (PropertyChanged != null)
  29. PropertyChanged(this, new PropertyChangedEventArgs(info));
  30. }
  31.  
  32. // Valueプロパティ
  33. private Int32 _Value;
  34. public Int32 Value
  35. {
  36. get { return _Value; }
  37. set {
  38. _Value = value;
  39. NotifyPropertyChanged("Value");
  40. }
  41. }
  42. }
  43. }

Int32をStringに変換するIValueConverter
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Text;
  4. using System.Windows;
  5. using System.Windows.Controls;
  6. using System.Windows.Data;
  7. using System.Windows.Documents;
  8. using System.Windows.Input;
  9. using System.Windows.Media;
  10. using System.Windows.Media.Imaging;
  11. using System.Windows.Shapes;
  12. using System.Globalization;
  13.  
  14. namespace Test
  15. {
  16. [ValueConversion(typeof(Int32), typeof(String))]
  17. public class NumberConverter
  18. : IValueConverter
  19. {
  20. public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
  21. {
  22. return ((Int32) value).ToString();
  23. }
  24.  
  25. public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
  26. {
  27. throw new NotImplementedException();
  28. }
  29. }
  30. }

そしてXAML.
  1. <Window
  2. xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  3. xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
  4. xmlns:sys="clr-namespace:System;assembly=mscorlib"
  5. xmlns:local="clr-namespace:Test"
  6. x:Class="Test.MainWindow"
  7. x:Name="Window"
  8. Title="MainWindow"
  9. Width="640" Height="480">
  10.  
  11. <Window.Resources>
  12. <local:Number x:Key="number"/>
  13. <local:NumberConverter x:Key="nc"/>
  14. </Window.Resources>
  15.  
  16. <StackPanel>
  17. <TextBlock HorizontalAlignment="Center" Margin="0,20,0,0" Text="{Binding Source={StaticResource number}, Path=Value, Converter={StaticResource nc}}"/>
  18. <StackPanel Orientation="Horizontal" HorizontalAlignment="Center">
  19. <Button Content="Add 10" Margin="10" Name="add"/>
  20. <Button Content="Subtract 10" Margin="10" Name="sub"/>
  21. </StackPanel>
  22. </StackPanel>
  23. </Window>

XAML.csはこんな感じ.
  1. public partial class MainWindow : Window
  2. {
  3. public MainWindow()
  4. {
  5. this.InitializeComponent();
  6.  
  7. Number num = (Number) this.FindResource("number");
  8.  
  9. this.add.Click += (e,s) => num.Value += 10;
  10. this.sub.Click += (e,s) => num.Value -= 10;
  11. }
  12. }



カテゴリ:WPF







記事メニュー
最近更新されたスレッド
ウィキ募集バナー