๐Ÿ‘ฉ๐Ÿป‍๐ŸŒพ

[C#/WPF] ๋ฒ„ํŠผ ๋ชจ์„œ๋ฆฌ ๋‘ฅ๊ธ€๊ฒŒ ๋งŒ๋“ค๊ธฐ ๋ณธ๋ฌธ

Language/C#

[C#/WPF] ๋ฒ„ํŠผ ๋ชจ์„œ๋ฆฌ ๋‘ฅ๊ธ€๊ฒŒ ๋งŒ๋“ค๊ธฐ

์ฅฌ์Šค์ด 2023. 1. 30. 10:51
728x90

์ด๋ฒˆ ํฌ์ŠคํŒ…์—์„œ๋Š” ๋ฒ„ํŠผ(Button)์˜ ๋ชจ์„œ๋ฆฌ๋ฅผ ๋‘ฅ๊ธ€๊ฒŒ ๋งŒ๋“œ๋Š” ๋ฒ•์— ๋Œ€ํ•ด ์จ๋ณด๋„๋ก ํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค.

 

WPF์—์„œ ๋ฒ„ํŠผ์„ ์ฒ˜์Œ ์ƒ์„ฑ์‹œ, ์ดˆ๊ธฐ ๋ชจ์„œ๋ฆฌ๊ฐ’์€ 0์œผ๋กœ ๊ฐ์ง„ ํ˜•ํƒœ์ž…๋‹ˆ๋‹ค. ์ด๋•Œ, ๋ชจ์„œ๋ฆฌ๋ฅผ ๋‘ฅ๊ธ€๊ฒŒ ํ•ด์ฃผ๋Š” ์†์„ฑ์ด ์žˆ๋Š”๋ฐ์š” ๋ฐ”๋กœ 'CornerRadius' ์ž…๋‹ˆ๋‹ค. ์ด ์†์„ฑ์€ ์†์„ฑ์ฐฝ์—์„œ๋Š” ์ฐพ์•„๋ณผ ์ˆ˜ ์—†๊ธฐ ๋•Œ๋ฌธ์— xaml์—์„œ ์ฝ”๋“œ๋กœ ์ž‘์„ฑํ•ด์„œ ๊ฐ’์„ ์„ค์ •ํ•ด์ฃผ์–ด์•ผ ํ•ฉ๋‹ˆ๋‹ค.


์šฐ์„ , ๋ฒ„ํŠผ์„ ํ•˜๋‚˜ ์ƒ์„ฑํ•ด์ค๋‹ˆ๋‹ค.

์ด์ œ xaml์—์„œ ํ•ด๋‹น ๋ฒ„ํŠผ์— ๋Œ€ํ•œ CornerRadius ์†์„ฑ๊ฐ’์„ ์„ค์ •ํ•ด์ฃผ๋„๋ก ํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค.

 

<Button>์— <Button.Resources> ํƒœ๊ทธ๋ฅผ ์ถ”๊ฐ€ํ•˜๊ณ  ํƒœ๊ทธ ์•ˆ์— TargetType์„ ๋ชจ์„œ๋ฆฌ๋กœ ์ง€์ •ํ•ด์ค๋‹ˆ๋‹ค. ๊ทธ๋ฆฌ๊ณ  Property๋ฅผ CornerRadius๋กœ ์„ค์ •ํ•˜๊ณ  ๊ฐ’์„ ์ž…๋ ฅํ•ด์ฃผ๋ฉด

์•„๋ž˜ ์‚ฌ์ง„๊ณผ ๊ฐ™์ด ๋ชจ์„œ๋ฆฌ๊ฐ€ ๋‘ฅ๊ธ€์–ด์ง„ ๊ฒƒ์„ ํ™•์ธํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

์ด๋ ‡๊ฒŒ ํ•˜๋‚˜์˜ ๋ฒ„ํŠผ์— ๋Œ€ํ•œ ์†์„ฑ์„ ๋ณ€๊ฒฝํ•  ๋•Œ๋Š” ํ•ด๋‹น ๋ฒ„ํŠผ์— ๋ฐ”๋กœ CornerRadius์˜ ์†์„ฑ์„ ์ถ”๊ฐ€ํ•ด์ฃผ๋ฉด ๋˜์ง€๋งŒ, ๋งŒ์•ฝ ๋ฒ„ํŠผ ์—ฌ๋Ÿฌ๊ฐœ์— ๊ฐ™์€ ์†์„ฑ๊ฐ’์„ ์ฃผ๊ธฐ ์œ„ํ•ด ๋ชจ๋“  ๋ฒ„ํŠผ์— ConerRadius์— ๋Œ€ํ•œ ์ฝ”๋“œ๋ฅผ ์“ฐ๊ฒŒ ๋œ๋‹ค๋ฉด

์œ„ ์‚ฌ์ง„์ฒ˜๋Ÿผ  ๋™์ผํ•œ ์ฝ”๋“œ์˜ ๋ฐ˜๋ณต๋ผ์„œ ์“ฐ์—ฌ์ ธ ์ฝ”๋“œ์˜ ๊ธธ์ด๊ฐ€ ๊ฝค ๋Š˜์–ด๋‚œ ๊ฒƒ์„ ํ™•์ธํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

 

๊ทธ๋ž˜์„œ ๊ฐ™์€ ์†์„ฑ์„ ๊ฐ€์ง„ ๋„๊ตฌ๋ฅผ ์—ฌ๋Ÿฌ๊ฐœ ์ƒ์„ฑํ•˜๊ณ  ์‹ถ์œผ๋ฉด ํ•ด๋‹น ๋„๊ตฌ์˜ ๋””์ž์ธ์„ ๋ฏธ๋ฆฌ ์„ค์ •ํ•ด์ฃผ๋ฉด ์•„๋ž˜ ์‚ฌ์ง„์ฒ˜๋Ÿผ ๋ณด๋‹ค ๊น”๋”ํ•˜๊ฒŒ ์ฝ”๋“œ๋ฅผ ์ž‘์„ฑํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

์ถ”๊ฐ€๋กœ CornerRadius๋ฟ๋งŒ ์•„๋‹ˆ๋ผ ๋‹ค๋ฅธ ์†์„ฑ๊ฐ’๋„ ์„ค์ •ํ•ด๋ณด์•˜์Šต๋‹ˆ๋‹ค.

 

+ ๋งˆ์ฐฌ๊ฐ€์ง€๋กœ ToggleButton๋„ ' Style TargetType = "{ x: ToggleButton }" '๋กœ๋งŒ ์ˆ˜์ •ํ•ด์ฃผ๋ฉด Button ๊ฐ™์ด ๋™์ผํ•˜๊ฒŒ ์ ์šฉํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

 

์ž‘์„ฑํ•œ xaml ์ „์ฒด ์ฝ”๋“œ์ž…๋‹ˆ๋‹ค.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
<Window x:Class="Example.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        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"
        xmlns:local="clr-namespace:Example"
        mc:Ignorable="d"
        Title="MainWindow" Height="450" Width="800">
    <Window.Resources>
        <Style TargetType="{x:Type Button}">
            <Setter Property="Width" Value="180"/>
            <Setter Property="Height" Value="160"/>
            <Setter Property="FontFamily" Value="/Example;component/Font/#KNPS Kkomi"/>
            <Setter Property="FontSize" Value="35"/>
            <Setter Property="Background" Value="#FFDFE4E0"/>
            <Style.Resources>
                <Style TargetType="{x:Type Border}">
                    <Setter Property="CornerRadius" Value="20"/>
                </Style>
            </Style.Resources>
        </Style>
    </Window.Resources>
    <Grid>
        <Button Content="Button1" HorizontalAlignment="Left" Margin="191,30,0,0" VerticalAlignment="Top"/>
        <Button Content="Button2" HorizontalAlignment="Left" Margin="191,234,0,0" VerticalAlignment="Top"/>
        <Button Content="Button3" HorizontalAlignment="Left" Margin="440,30,0,0" VerticalAlignment="Top"/>
        <Button Content="Button4" HorizontalAlignment="Left" Margin="440,234,0,0" VerticalAlignment="Top"/>
    </Grid>
</Window>
cs

 

728x90
Comments