[C# WPF] Page 내에서 공통 적용되는 Button Style

우롱밀크티당도70·2023년 9월 5일
0

WPF

목록 보기
5/22
post-thumbnail
post-custom-banner

1. 개발환경

  • VisualStudio 2022 / .NET Framework 4.7.2
  • WPF

2. 내용

한 Page 내에서 Style이 같은 여러 개의 Button을 배치할 때...

	<Page.Resources>
        <Style TargetType="Button">
            <Setter Property="Template">
                <Setter.Value>
                    <ControlTemplate TargetType="Button">
                        <Border Background="{TemplateBinding Background}" CornerRadius="3">
                            <Grid>
                                <ContentPresenter HorizontalAlignment="Center" VerticalAlignment="Center" />
                            </Grid>
                        </Border>
                    </ControlTemplate>
                </Setter.Value>
            </Setter>
            <Setter Property="Background" Value="Blue" />
            <Setter Property="Foreground" Value="White" />
            <Setter Property="Cursor" Value="Hand" />
        </Style>

    </Page.Resources>
    
    <Grid>
        <Button Content="Button" Width="150" Height="45" FontFamily="Arial" Margin="186,198,464,207" />
        <Button Content="Button2" Width="150" Height="45" FontFamily="Arial" Margin="341,198,309,207" />
        <Button Content="Button3" Width="150" Height="45" FontFamily="Arial" Margin="496,198,154,207" />
    </Grid>

또는 x:Key로 이름을 지정하여 특정 버튼만 Style을 적용할 수 있다.

	<Page.Resources>
        <Style x:Key="ButtonStyle1" TargetType="Button">
            <Setter Property="Template">
                <Setter.Value>
                    <ControlTemplate TargetType="Button">
                        <Border Background="{TemplateBinding Background}" CornerRadius="3">
                            <Grid>
                                <ContentPresenter HorizontalAlignment="Center" VerticalAlignment="Center" />
                            </Grid>
                        </Border>
                    </ControlTemplate>
                </Setter.Value>
            </Setter>
            <Setter Property="Background" Value="Blue" />
            <Setter Property="Foreground" Value="White" />
            <Setter Property="Cursor" Value="Hand" />
        </Style>

    </Page.Resources>
    
    <Grid>
        <Button Content="Button" Width="150" Height="45" FontFamily="Arial" Margin="186,198,464,207" />
        <Button Style="{StaticResource ButtonStyle1}" Content="Button2" Width="150" Height="45" FontFamily="Arial" Margin="341,198,309,207" />
        <Button Content="Button3" Width="150" Height="45" FontFamily="Arial" Margin="496,198,154,207" />
    </Grid>
profile
안뇽하세용
post-custom-banner

0개의 댓글