「東雲 忠太郎」の平凡な日常のできごと

「東雲 忠太郎」の平凡な日常のできごと

2024.06.09
XML
カテゴリ: WPFC#.NET


標準のコントロールをカスタマイズする方法はいくつかありますが、ここでは簡単な例を紹介します。例として、ボタンの背景色やテキストの色を変更する方法を示します。


```xml

<Window x:Class="WpfApp.MainWindow"


        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"

        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"

        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"

        mc:Ignorable="d"

        Title="MainWindow" Height="450" Width="800">

    <Window.Resources>

        <Style x:Key="CustomButtonStyle" TargetType="Button">

            <Setter Property="Background" Value="LightBlue"/>

            <Setter Property="Foreground" Value="Black"/>

            <Setter Property="FontWeight" Value="Bold"/>

            <Setter Property="FontSize" Value="16"/>

            <Setter Property="Padding" Value="10"/>

            <Setter Property="BorderThickness" Value="2"/>

            <Setter Property="BorderBrush" Value="DarkBlue"/>

            <Setter Property="HorizontalAlignment" Value="Center"/>

            <Setter Property="VerticalAlignment" Value="Center"/>

            <Setter Property="Cursor" Value="Hand"/>

            <Setter Property="Template">

                <Setter.Value>

                    <ControlTemplate TargetType="Button">

                        <Border Background="{TemplateBinding Background}" 

                                BorderBrush="{TemplateBinding BorderBrush}" 

                                BorderThickness="{TemplateBinding BorderThickness}" 

                                Padding="{TemplateBinding Padding}">

                            <ContentPresenter HorizontalAlignment="Center" VerticalAlignment="Center"/>

                        </Border>

                    </ControlTemplate>

                </Setter.Value>

            </Setter>

            <Style.Triggers>

                <Trigger Property="IsMouseOver" Value="True">

                    <Setter Property="Foreground" Value="White"/>

                    <Setter Property="Background" Value="DarkBlue"/>

                </Trigger>

            </Style.Triggers>

        </Style>

    </Window.Resources>

    <Grid>

        <Button Content="Click Me" Style="{StaticResource CustomButtonStyle}" Width="150" Height="50"/>

    </Grid>

</Window>

```


この例では、`Style`リソースを定義して、ボタンの見た目をカスタマイズしています。このスタイルは、`TargetType`が`Button`に設定されているため、すべてのボタンに適用されます。スタイルは背景色、テキスト色、フォントの太さやサイズ、パディング、ボーダーの太さや色などを定義しています。


また、ボタンがマウスオーバーされたときに色が変わるトリガーも定義されています。


このように、XAMLで`Style`を使用してコントロールの見た目をカスタマイズすることができます。必要に応じて、より複雑なカスタマイズや他のコントロールにも同じように適用できます。






お気に入りの記事を「いいね!」で応援しよう

Last updated  2024.06.09 13:44:21


【毎日開催】
15記事にいいね!で1ポイント
10秒滞在
いいね! -- / --
おめでとうございます!
ミッションを達成しました。
※「ポイントを獲得する」ボタンを押すと広告が表示されます。
x

© Rakuten Group, Inc.
X
Design a Mobile Website
スマートフォン版を閲覧 | PC版を閲覧
Share by: