Styleとプロパティ
Styleとプロパティ
XAMLで記述する内容は、タグとプロパティでできています。
タグは、FrameworkElementを継承したクラスです。
プロパティは、そのクラスが公開しているプロパティです。
このプロパティは、Styleでも設定できるようになっています。
- <TextBlock Text="ABC" Foreground="Red">
- <TextBlock.Style>
- <Style TargetType="TextBlock">
- <Setter Property="Foreground" Value="Green"/>
- </Style>
- </TextBlock.Style>
- </TextBlock>
この場合、実際に表示される文字列の色は赤になります。
Styleとプロパティの関係を見るために、簡単なクラスを作ってみます。
Class1クラスに、2つのプロパティ Property1、Property2 を作り、それを以下のXAMLで使用します。
- <Grid Loaded="Grid_Loaded"
- xmlns:u="clr-namespace:WpfApplication1">
- <u:Class1 Property1="1 2 3">
- <u:Class1.Style>
- <Style TargetType="u:Class1">
- <Setter Property="Property2" Value="9 8 7"/>
- </Style>
- </u:Class1.Style>
- </u:Class1>
- </Grid>
Property1は、プロパティとして値を代入し、Property2には、Setterで値をセットしています。
SettertでProperty1を設定するのは、実行時にエラーとなります。
- public class Class1 : FrameworkElement
- {
- public DoubleCollection Property1 { get; set; }
- public DoubleCollection Property2 { get; set; }
- // このプロパティを依存関係プロパティに登録
- public static readonly DependencyProperty Property2Property =
- DependencyProperty.Register(
- "Property2",
- typeof(DoubleCollection),
- typeof(Class1),
- new PropertyMetadata(Property2PropertyChanged));
- // 依存関係プロパティの変更イベント(スタティック)
- static void Property2PropertyChanged(DependencyObject d,
- DependencyPropertyChangedEventArgs e)
- {
- ((Class1)d).Property2 =
- (DoubleCollection)e.NewValue;
- }
- public Class1()
- {
- Loaded += new RoutedEventHandler(Class1_Loaded);
- }
- void Class1_Loaded(object sender, RoutedEventArgs e)
- {
- Debug.WriteLine(Property1);
- Debug.WriteLine(Property2);
- }
- }
Setterで扱うのは、依存関係プロパティであることが必要なようです。
実際の実行順は、プロパティの設定、Setterによる設定の順に行われます。
これは、最初の TextBlockの Foreground の例の結果と矛盾します。
本当のところはわかりませんが、初期化中にプロパティで設定された値は、初期化の間は変更されないように動作するものと解釈しておきます。
Styleの実態1
前述のClass1に対するStyleの設定は、Class1のStyleプロパティを設定することは明白です。
以下の場合は、どうでしょうか。
- <Grid Loaded="Grid_Loaded"
- xmlns:u="clr-namespace:WpfApplication1">
- <Grid.Resources>
- <Style TargetType="u:Class1">
- <Setter Property="Property2" Value="9 8 7"/>
- </Style>
- </Grid.Resources>
- <u:Class1 x:Name="C1" Property2="1 2 3"/>
- <u:Class1 x:Name="C2"/>
- <u:Class1 x:Name="C3">
- <u:Class1.Style>
- <Style TargetType="u:Class1">
- <Setter Property="Property2" Value="7 7 7"/>
- </Style>
- </u:Class1.Style>
- </u:Class1>
- </Grid>
この記述によって、3つのClass1型のインスタンス C1、C2、C3 が生成されます。
Class1のLoadedイベント処理を以下のように修正して、どのインスタンスか区別がつくようにします。
- void Class1_Loaded(object sender, RoutedEventArgs e)
- {
- Debug.WriteLine(Name + " " + Property2);
- }
MainWindowの Loadedイベント処理に以下の行を入れて、インスタンスの同一性を確認します。
- Debug.WriteLine(ReferenceEquals(C1.Style, C2.Style));
- Debug.WriteLine(ReferenceEquals(C1.Style, C3.Style));
結果は、以下のようになりました。
- True
- False
- C1 1 2 3
- C2 9 8 7
- C3 7 7 7
-
Resourceで定義したStyleは1つのインスタンスが生成され、Style宣言されていないClass1インスタンスのStyleに設定される
-
実際の設定値は、Styleよりプロパティの設定が優先される
Styleの実態2
「Styleの実態1」で確認したことを、コードで再確認します。
- <StackPanel Name="stackpanel1">
- <StackPanel.Resources>
- <Style x:Key="ellipse_style1" TargetType="Control">
- <Setter Property="Background" Value="Cyan"/>
- <Setter Property="Template">
- <Setter.Value>
- <ControlTemplate>
- <Ellipse Fill="{TemplateBinding Background}"/>
- </ControlTemplate>
- </Setter.Value>
- </Setter>
- </Style>
- </StackPanel.Resources>
- </StackPanel>
この Style を使って、直径の違う円を2つ描くことを考えます。
- Control c = new Control();
- Style style = (Style)stackpanel1.Resources["ellipse_style1"];
- style.Setters.Add(new Setter(WidthProperty, 20.0));
- style.Setters.Add(new Setter(HeightProperty, 20.0));
- c.Style = style;
- stackpanel1.Children.Add(c);
- Control c2 = new Control();
- Style style2 = (Style)stackpanel1.Resources["ellipse_style1"];
- //style2.Setters.Add(new Setter(WidthProperty, 40.0));
- //style2.Setters.Add(new Setter(HeightProperty, 40.0));
- c2.Style = style2;
- stackpanel1.Children.Add(c2);
実際には、2回目のサイズ設定でエラーが起きます。これをコメントにすると、同じサイズの円が2つ描かれることになります。
XAML記述からは、Style のインスタンスが、stackpanel1.Resources["ellipse_style1"] で参照される場所に作られます。
Width、Height の設定は、この Setters に、WidthProperty、HedthProperty を加える操作になります。
このスタイルを、1度FrameworkElementのStyleに設定すると、シールされ変更できなくなります。
このため、2度目のサイズ設定が失敗します。
-
FrameworkElement の Style プロパティに設定されている、スタイルのインスタンスは、シールされていて変更できない。
FrameworkElement の Style プロパティは、変更できる。スタイルを変えるには、新しいスタイルをプロパティに代入する。
-
XAML記述からResourcesに作られるのは、そのインスタンスそのもの。XAML記述のようなインスタンスを作る元ではない。
-
例のように、複数のインスタンスを生成する必要がある場合には、クローン、ディープコピー、シリアライズと言った方法が考えられるが有効な方法は見つからない。
-
おそらく、XAML記述からFrameworkElementをその都度生成するのが正攻法。
WPFだけにある機能ですが、FrameworkElementFactoryを使う解決方法もあります。「FrameworkElementFactory(コードでTemplateを作る)」
参照
問題を整理すると、
-
XAMLを分割して記述する方法
-
記述1つから複数のインスタンスを得る方法
と、言うことになります。
「カスタムコントロールのXAMLとコード」
「Styleとプロパティ」
「Styleとバインディング」
「DataContextとインスタンス」
「XAML記述とVisualの変換」
「1つのStyle記述から複数のインスタンス」
「FrameworkElementFactory(コードでTemplateを作る)」
|