LoginSignup
8
9

More than 5 years have passed since last update.

XAML:StaticResourceとStyleの利用

Posted at

準備

<sys:Double><sys:String>を利用するために

MainWindow.xaml
<Window 
  x:Class="WpfApplicationTest1.MainWindow"
  ...省略...
  xmlns:sys="clr-namespace:System;assembly=mscorlib"  // 追加
  ...省略...
>

StaticResource

StaticResourceを利用してボタンのプロパティを設定

MainWindow.xaml
  <Window.Resources>
    <sys:Double x:Key="BT_Width">120</sys:Double>
    <sys:Double x:Key="BT_Height">30</sys:Double>
    <sys:String x:Key="BT_Text">TestButton</sys:String>
  </Window.Resources>

  <StackPanel>
    <Button 
      Width="{StaticResource BT_Width}" 
      Height="{StaticResource BT_Height}" 
      Content="{StaticResource BT_Text}">
    </Button>
  </StackPanel>

StaticResource & Style

スタイルを定義して適用

MainWindow.xaml
  <Window.Resources>
    <sys:Double x:Key="BT_Width">120</sys:Double>
    <sys:Double x:Key="BT_Height">30</sys:Double>
    <sys:String x:Key="BT_Text">TestButton</sys:String>
    <SolidColorBrush x:Key="BT_TextColor" Color="Red"/>
    <Style x:Key="BT_StyleA" TargetType="{x:Type Button}">
      <Setter Property="Width" Value="{StaticResource BT_Width}" />
      <Setter Property="Height" Value="{StaticResource BT_Height}" />
      <Setter Property="Foreground" Value="{StaticResource BT_TextColor}"/>
    </Style>
  </Window.Resources>

  <StackPanel Orientation="Horizontal">
    <Button Style="{StaticResource BT_StyleA}" Content="A"/>
    <Button Style="{StaticResource BT_StyleA}" Content="B"/>
  </StackPanel>

Styleの継承

MainWindow.xaml
  <Window.Resources>
    <sys:Double x:Key="BT_Width">120</sys:Double>
    <sys:Double x:Key="BT_Height">30</sys:Double>
    <sys:String x:Key="BT_Text">TestButton</sys:String>
    <SolidColorBrush x:Key="BT_TextColorInfo" Color="Blue"/>
    <SolidColorBrush x:Key="BT_TextColorWarning" Color="Red"/>
    <Style x:Key="BT_BaseStyle" TargetType="{x:Type Button}">
      <Setter Property="Width" Value="{StaticResource BT_Width}" />
      <Setter Property="Height" Value="{StaticResource BT_Height}" />
    </Style>
    <Style x:Key="BT_StyleInfo" TargetType="{x:Type Button}" BasedOn="{StaticResource BT_BaseStyle}">
      <Setter Property="Foreground" Value="{StaticResource BT_TextColorInfo}" />
    </Style>
    <Style x:Key="BT_StyleWarning" TargetType="{x:Type Button}" BasedOn="{StaticResource BT_BaseStyle}">
      <Setter Property="Foreground" Value="{StaticResource BT_TextColorWarning}" />
    </Style>

  </Window.Resources>

  <StackPanel Orientation="Horizontal">
    <Button Style="{StaticResource BT_StyleInfo}" Content="A"/>
    <Button Style="{StaticResource BT_StyleWarning}" Content="B"/>
  </StackPanel>

リソースを外部ファイルに移動する

「新しい項目の追加」→「リソースディクショナリ(WPF)」→「Dictionary1.xaml」

Dictionary1.xaml
<ResourceDictionary
  xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
  xmlns:local="clr-namespace:WpfApplicationTest1"
  xmlns:sys="clr-namespace:System;assembly=mscorlib"> // 追加!
  <sys:Double x:Key="BT_Width">120</sys:Double>
  ...省略...
</ResourceDictionary>
MainWindow.xaml
  <Window.Resources>
    <ResourceDictionary Source="Dictionary1.xaml" />
  </Window.Resources>

  <StackPanel Orientation="Horizontal">
    <Button Style="{StaticResource BT_StyleInfo}" Content="A"/>
    <Button Style="{StaticResource BT_StyleWarning}" Content="B"/>
  </StackPanel>

MainWindow.xamlからはxmlns:sys="clr-namespace:System;assembly=mscorlib"を削除しても大丈夫。

8
9
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
8
9