본문 바로가기
프로그래밍/UWP | WPF

[WPF] Circle gradient brush 설정하기

by 남생 namsaeng 2022. 8. 19.
반응형

<MainPage.xaml>

<Page x:Class="WpfTestProject.MainPage"
      xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
      xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
      xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
      xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
      xmlns:local="clr-namespace:WpfTestProject"
      mc:Ignorable="d" 
      d:DesignHeight="450" d:DesignWidth="800"
      Title="MainPage">

    <Grid>
        <Menu VerticalAlignment="Top" >
            <MenuItem Header="Mode" >
                <MenuItem Header="Open"/>
                <MenuItem Header="Close" />
            </MenuItem>
        </Menu>
        <Grid>
            <Grid.ColumnDefinitions>
                <ColumnDefinition Width="*"/>
            </Grid.ColumnDefinitions>
            <Grid.RowDefinitions>
                <RowDefinition Height="0.02*"/>
                <RowDefinition Height="0.1*"/>
                <RowDefinition Height="*"/>
            </Grid.RowDefinitions>

            <Border Grid.Row="2" Background="White">
                <Grid>
                    <Grid.RowDefinitions>
                        <RowDefinition Height="0.5*"/>
                        <RowDefinition Height="*"/>
                    </Grid.RowDefinitions>
                    <Grid.ColumnDefinitions>
                        <ColumnDefinition Width="*"/>
                        <ColumnDefinition Width="*"/>
                    </Grid.ColumnDefinitions>
                    <Border Grid.Row="0" Grid.ColumnSpan="2">
                        <Border.Background>
                            <LinearGradientBrush EndPoint="0.5,1" StartPoint="0.5,0">
                                <GradientStop Color="Orange"/>
                                <GradientStop Color="Yellow" Offset="0.625"/>
                            </LinearGradientBrush>
                        </Border.Background>
                        <StackPanel VerticalAlignment="Center">
                            <TextBlock Text="Sipping Game" FontSize="35" FontWeight="Bold" VerticalAlignment="Center" HorizontalAlignment="Center"/>
                            <TextBlock Text="Odd makes Green / Even makes Red" FontSize="35" FontWeight="Bold" VerticalAlignment="Center" HorizontalAlignment="Center"/>
                        </StackPanel>
                    </Border>
                    <Border Grid.Row="1" Grid.Column="0">
                        <StackPanel VerticalAlignment="Center">
                            <Ellipse x:Name="Result"  Width="120" Height="120">
                                <Ellipse.Fill>
                                    <RadialGradientBrush GradientOrigin="0.75 0.25">
                                        <GradientStop Offset="0.0" Color="White" />
                                        <GradientStop Offset="0.25" Color="LightGray" />
                                        <GradientStop Offset="1.0" Color="Gray" />
                                    </RadialGradientBrush>
                                </Ellipse.Fill>
                            </Ellipse>
                            <TextBlock x:Name="Result_Textblock" Text="Not Result" FontSize="35" FontWeight="Bold" VerticalAlignment="Center" HorizontalAlignment="Center"/>
                        </StackPanel>
                    </Border>
                    <Border Grid.Row="1" Grid.Column="1">   
                        <Button x:Name="Toss_Button" Background="Magenta" VerticalAlignment="Center" HorizontalAlignment="Center" BorderBrush="LightYellow" BorderThickness="7" Height="100" Width="200" Click="Toss_Button_Click">
                            <TextBlock Foreground="Black" FontSize="25" FontWeight="Bold">Toss a coin!</TextBlock>
                        </Button>
                    </Border>
                </Grid>
            </Border>
        </Grid>
    </Grid>
</Page>

 

<MainPage.xaml.cs>

namespace WpfTestProject
{
    public partial class MainPage : Page
    {
        RadialGradientBrush GreenRadialGradientBrush;
        RadialGradientBrush RedRadialGradientBrush;
        public MainPage()
        {
            InitializeComponent();

            GreenRadialGradientBrush = new RadialGradientBrush();
            GreenRadialGradientBrush.GradientOrigin = new Point(0.75, 0.25);
            GreenRadialGradientBrush.GradientStops.Add(new GradientStop(Colors.LightGreen, 0.0));
            GreenRadialGradientBrush.GradientStops.Add(new GradientStop(Colors.ForestGreen, 0.25));
            GreenRadialGradientBrush.GradientStops.Add(new GradientStop(Colors.Green, 1.0));

            RedRadialGradientBrush = new RadialGradientBrush();
            RedRadialGradientBrush.GradientOrigin = new Point(0.75, 0.25);
            RedRadialGradientBrush.GradientStops.Add(new GradientStop(Colors.Yellow, 0.0));
            RedRadialGradientBrush.GradientStops.Add(new GradientStop(Colors.Orange, 0.25));
            RedRadialGradientBrush.GradientStops.Add(new GradientStop(Colors.Red, 1.0));
        }

        private void Toss_Button_Click(object sender, RoutedEventArgs e)
        {
            int isSucceed;

            Random r = new Random();
            isSucceed = r.Next(0, 2);

            if (isSucceed == 1)
            {
                this.Result_Textblock.Text = "It's an odd number";
                this.Result.Fill = GreenRadialGradientBrush;

            }
            else
            {
                this.Result_Textblock.Text = "It's an even number";
                this.Result.Fill = RedRadialGradientBrush;
            }
        }
    }
}

 

 

 

 

WPF Circle gradient brush
WPF Circle Gradient Brush

반응형

댓글