Styleとバインディング
プロパティ自体は、プロパティとして記述するか、StyleのSetterでアクセスするかで、宣言の仕方が違いました。
次に、Styleで値をバインディングする場合を見ます。Styleとプロパティで差があるのかどうかはわかりません。
XAMLとビハインドコード
ウイザードが作るXAMLとビハインドコードの場合を見ます。
- <Grid Loaded="Grid_Loaded">
- <Grid.Resources>
- <Style TargetType="Button">
- <Setter Property="Content" Value="{Binding BindingValue1}"/>
- </Style>
- </Grid.Resources>
- <StackPanel>
- <Button/>
- <Button Content="{Binding BindingValue2}"/>
- </StackPanel>
- </Grid>
バインドする値は、以下のように宣言しました。
- string _BindingValue1;
- public string BindingValue1
- {
- get { return _BindingValue1; }
- set
- {
- _BindingValue1 = value;
- if(PropertyChanged!=null)
- PropertyChanged.Invoke(this,
- new PropertyChangedEventArgs("BindingValue1"));
- }
- }
- string _BindingValue2;
- public string BindingValue2
- {
- get { return _BindingValue2; }
- set
- {
- _BindingValue2 = value;
- if (PropertyChanged != null)
- PropertyChanged.Invoke(this,
- new PropertyChangedEventArgs("BindingValue2"));
- }
- }
Loadedイベントで、値をセットします。
- private void Grid_Loaded(object sender, RoutedEventArgs e)
- {
- DataContext = this;
- BindingValue1 = "VALUE1";
- BindingValue2 = "VALUE2";
- }
DataContext に this を代入しないと上手く行きません。
このことは、以下のようにペアの関係に無関係にバインディングできることを示しています。
- public class XXX
- {
- public string BindingValue1 { get; set; }
- public string BindingValue2 { get; set; }
- public XXX()
- {
- BindingValue1 = "XXX1";
- BindingValue2 = "XXX2";
- }
- }
以下のようにバインドできます。
- private void Grid_Loaded(object sender, RoutedEventArgs e)
- {
- DataContext = new XXX();
- }
また、DataContext は、XAMLでも記述できます。この場合、コードでは何も設定しなくてもXXXクラスのコンストラクタで設定した値がボタンに表示されます。
- <Grid xmlns:u="clr-namespace:WpfApplication1" Loaded="Grid_Loaded">
- <Grid.Resources>
- <u:XXX x:Key="xxx"/>
- <Style TargetType="Button">
- <Setter Property="Content"
- Value="{Binding Source={StaticResource xxx},
- Path=BindingValue1}"/>
- </Style>
- </Grid.Resources>
- <StackPanel DataContext="{Binding Source={StaticResource xxx}}">
- <Button/>
- <Button Content="{Binding BindingValue2}"/>
- </StackPanel>
- </Grid>
個別のXAMLファイルとコードの場合
XAMLファイルとコードを別々に作成した場合にはどうなるのか見ます。
TEST2.xaml を以下の内容で作成します。
- <ContentControl
- xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
- xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
- <Grid>
- <Grid.Resources>
- <Style TargetType="Button">
- <Setter Property="Content"
- Value="{Binding BindingValue1}"/>
- </Style>
- </Grid.Resources>
- <StackPanel>
- <Button x:Name="B1"/>
- <Button x:Name="B2" Content="{Binding BindingValue2}"/>
- <Button x:Name="B3">
- <Button.Style>
- <Style TargetType="Button">
- <Setter Property="Content"
- Value="{Binding BindingValue3}"/>
- </Style>
- </Button.Style>
- </Button>
- </StackPanel>
- </Grid>
- </ContentControl>
TEST3.cs を以下の内容で作成します。
- using System;
- using System.Windows;
- using System.Windows.Controls;
- using System.Windows.Markup;
- using System.Diagnostics;
- namespace WpfApplication1
- {
- public class TEST3 : ContentControl, IComponentConnector
- {
- public TEST3()
- {
- BindingValue1 = "BBB1";
- BindingValue2 = "BBB2";
- BindingValue3 = "BBB3";
- Uri resourceLocater
- = new Uri("/WpfApplication1;component/TEST2.xaml",
- System.UriKind.Relative);
- Application.LoadComponent(this, resourceLocater);
- Loaded += new RoutedEventHandler(TEST3_Loaded);
- }
- public string BindingValue1 { get; set; }
- public string BindingValue2 { get; set; }
- public string BindingValue3 { get; set; }
- void TEST3_Loaded(object sender, RoutedEventArgs e)
- {
- DataContext = this;
- }
- void IComponentConnector.InitializeComponent()
- {
- Debug.WriteLine("IComponentConnector.InitializeComponent()");
- }
- public void Connect(int connectionId, object target)
- {
- Debug.WriteLine("Connect " + target);
- }
- }
- }
これをMainWindowでインスタンスして表示します。
- private void Grid_Loaded(object sender, RoutedEventArgs e)
- {
- TEST3 test3 = new TEST3();
- stackpanel1.Children.Add(test3);
- }
MainWindow.xaml には以下のように、stackpanel1 と名前を付けた StackPanel を起きます。
- <Grid Loaded="Grid_Loaded">
- <StackPanel Name="stackpanel1">
- </StackPanel>
- </Grid>
実行すると左図を表示します。
XAMLとビハインドコードのペアと同様に考えて良さそうです。
やはり、DataContext の設定が必要です。
Loadedイベントで、DataContext の設定の前後でボタンのContentsの内容を見ると、DataContext の設定の前には値が設定されていません。
DataContextの設定で、ボタンのContentsの値が変わりますが、この値はButtonインスタンス内にあるのか、Contentsプロパティの参照先が変更されたのかは区別がつきません。
参照
問題を整理すると、
-
XAMLを分割して記述する方法
-
記述1つから複数のインスタンスを得る方法
と、言うことになります。
「カスタムコントロールのXAMLとコード」
「Styleとプロパティ」
「Styleとバインディング」
「DataContextとインスタンス」
「XAML記述とVisualの変換」
「1つのStyle記述から複数のインスタンス」
「FrameworkElementFactory(コードでTemplateを作る)」
|