エッセンシャルWPF:Windows Presentation Foundation (Programmer's SELECTION)
- 作者: Chris Anderson,星睦
- 出版社/メーカー: 翔泳社
- 発売日: 2007/10/31
- メディア: 大型本
- 購入: 6人 クリック: 128回
- この商品を含むブログ (32件) を見る
- 作者: 高橋忍,川西裕幸
- 出版社/メーカー: ソフトバンク クリエイティブ
- 発売日: 2007/04/07
- メディア: 大型本
- 購入: 3人 クリック: 158回
- この商品を含むブログ (26件) を見る
WPFトリガー使用方法
Windows1.xaml
<Window x:Class="WpfTrigger.Window1" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Title="Window1" Height="300" Width="300"> <Window.Resources> <Style TargetType="Button"> <Style.Triggers> <Trigger Property="IsMouseOver" Value="true"> <Trigger.Setters> <Setter Property = "Height" Value="40"/> </Trigger.Setters> </Trigger> </Style.Triggers> </Style> </Window.Resources> <StackPanel> <Button Width="120">高さが変化します</Button> </StackPanel> </Window>
Windows1.xaml.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 WpfTrigger { /// <summary> /// Window1.xaml の相互作用ロジック /// </summary> public partial class Window1 : Window { public Window1() { InitializeComponent(); } } }
WPF - RadioButtonバインド方法
Windows.xaml
<Window x:Class="WpfMsControl.Window2" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:local="clr-namespace:WpfMsControl" Title="Window1" Height="227" Width="300"> <Window.Resources> <local:EnumRadioConverter x:Key="EnumRadioConverter"/> </Window.Resources> <Grid> <GroupBox Header="ラジオボタンバインド"> <StackPanel > <Button x:Name="Button" Margin="10" Click="Button_Click">確認ボタン</Button> <RadioButton IsChecked="{Binding Path=MyType, Converter={StaticResource EnumRadioConverter}, ConverterParameter=TYPE_A}" Margin="10" GroupName="ButtonVisibility">タイプA</RadioButton> <RadioButton IsChecked="{Binding Path=MyType, Converter={StaticResource EnumRadioConverter}, ConverterParameter=TYPE_B}" Margin="10" GroupName="ButtonVisibility">タイプB</RadioButton> <RadioButton IsChecked="{Binding Path=MyType, Converter={StaticResource EnumRadioConverter}, ConverterParameter=TYPE_C}" Margin="10" GroupName="ButtonVisibility">タイプC</RadioButton> <RadioButton IsChecked="{Binding Path=MyType, Converter={StaticResource EnumRadioConverter}, ConverterParameter=TYPE_D}" Margin="10" GroupName="ButtonVisibility">タイプD</RadioButton> </StackPanel> </GroupBox> </Grid> </Window>
Windows2.xaml.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.Shapes; namespace WpfMsControl { /// <summary> /// Window2.xaml の相互作用ロジック /// </summary> public partial class Window2 : Window { TestData data; public Window2() { InitializeComponent(); //(1)数値をEnum値変換した値を指定 //Type type = typeof(EnumDefines.MyEnum); //EnumDefines.MyEnum myEnum = (EnumDefines.MyEnum)Enum.Parse(type, "1", true); //(2) Enum値を指定 this.data = new TestData(EnumDefines.MyEnum.TYPE_B); //データ設定 this.DataContext = data; } private void Button_Click(object sender, RoutedEventArgs e) { MessageBox.Show(this.data.MyType.ToString()); } } }
EnumRadioConverter.cs
using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace WpfMsControl { public class EnumRadioConverter : System.Windows.Data.IValueConverter { public object Convert(object value, System.Type targetType, object parameter, System.Globalization.CultureInfo culture) { var paramString = parameter as string; if (paramString == null) { return System.Windows.DependencyProperty.UnsetValue; } if (!Enum.IsDefined(value.GetType(), paramString)) { return System.Windows.DependencyProperty.UnsetValue; } var paramParsed = Enum.Parse(value.GetType(), paramString); return (value.Equals(paramParsed)); } public object ConvertBack(object value, System.Type targetType, object parameter, System.Globalization.CultureInfo culture) { var paramString = parameter as string; if (paramString == null) { return System.Windows.DependencyProperty.UnsetValue; } return Enum.Parse(targetType, paramString); } } }
EnumDefines.cs
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.ComponentModel; namespace WpfMsControl { public class EnumDefines { public enum MyEnum { TYPE_A, TYPE_B, TYPE_C, TYPE_D, } } }
TestData.cs
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.ComponentModel; namespace WpfMsControl { public class TestData : INotifyPropertyChanged { public event PropertyChangedEventHandler PropertyChanged; private EnumDefines.MyEnum myType; public EnumDefines.MyEnum MyType { get { return this.myType; } set { this.myType = value; this.NotifyChanged("MyType"); } } public TestData(EnumDefines.MyEnum myType) { this.myType = myType; } void NotifyChanged(string property) { if (PropertyChanged != null) { PropertyChanged(this, new PropertyChangedEventArgs(property)); } } } }