■ Grid 엘리먼트에서 중첩 그리드를 사용하는 방법을 보여준다.
▶ DoubleToStringConverter.cs
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 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 |
using System.Globalization; namespace Testproject; /// <summary> /// 실수→문자열 변환자 /// </summary> public class DoubleToStringConverter : IValueConverter { //////////////////////////////////////////////////////////////////////////////////////////////////// Method ////////////////////////////////////////////////////////////////////////////////////////// Public #region 변환하기 - Convert(sourceValue, targetType, parameter, cultureInfo) /// <summary> /// 변환하기 /// </summary> /// <param name="sourceValue">소스 값</param> /// <param name="targetType">타겟 타입</param> /// <param name="parameter">매개 변수</param> /// <param name="cultureInfo">문화 정보</param> /// <returns>변환 값</returns> public object Convert(object sourceValue, Type targetType, object parameter, CultureInfo cultureInfo) { if(int.TryParse((string)parameter, out int decimalPoint)) { return ((double)sourceValue).ToString($"f{decimalPoint}"); } else { return ((double)sourceValue).ToString(); } } #endregion #region 역변환하기 - ConvertBack(sourceValue, targetType, parameter, cultureInfo) /// <summary> /// 역변환하기 /// </summary> /// <param name="sourceValue">소스 값</param> /// <param name="targetType">타겟 타입</param> /// <param name="parameter">매개 변수</param> /// <param name="cultureInfo">문화 정보</param> /// <returns>역변환 값</returns> public object ConvertBack(object sourceValue, Type targetType, object parameter, CultureInfo cultureInfo) { throw new NotImplementedException(); } #endregion } |
▶ MainPage.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 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 |
<?xml version="1.0" encoding="utf-8" ?> <ContentPage x:Class="TestProject.MainPage" xmlns="http://schemas.microsoft.com/dotnet/2021/maui" xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml" xmlns:local="clr-namespace:Testproject"> <ContentPage.Resources> <local:DoubleToStringConverter x:Key="DoubleToStringConverterKey" /> <Style TargetType="Label"> <Setter Property="HorizontalTextAlignment" Value="Center" /> </Style> </ContentPage.Resources> <Grid> <Grid.RowDefinitions> <RowDefinition Height="500" /> <RowDefinition Height="Auto" /> </Grid.RowDefinitions> <BoxView x:Name="boxView" Color="Black" /> <Grid Grid.Row="1" Margin="20" RowDefinitions="Auto,Auto,Auto,Auto,Auto,Auto"> <Slider x:Name="redSlider" /> <Label Grid.Row="1" Text="{Binding Source={x:Reference redSlider}, Path=Value, ConverterParameter=2, Converter={StaticResource DoubleToStringConverterKey}, StringFormat='적색 : {0}'}" /> <Slider x:Name="greenSlider" Grid.Row="2" /> <Label Grid.Row="3" Text="{Binding Source={x:Reference greenSlider}, Path=Value, ConverterParameter=2, Converter={StaticResource DoubleToStringConverterKey}, StringFormat='녹색 : {0}'}" /> <Slider x:Name="blueSlider" Grid.Row="4" /> <Label Grid.Row="5" Text="{Binding Source={x:Reference blueSlider}, Path=Value, ConverterParameter=2, Converter={StaticResource DoubleToStringConverterKey}, StringFormat='청색 : {0}'}" /> </Grid> </Grid> </ContentPage> |
▶ MainPage.xaml.cs
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 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 |
namespace TestProject; /// <summary> /// 메인 페이지 /// </summary> public partial class MainPage : ContentPage { //////////////////////////////////////////////////////////////////////////////////////////////////// Constructor ////////////////////////////////////////////////////////////////////////////////////////// Public #region 생성자 - MainPage() /// <summary> /// 생성자 /// </summary> public MainPage() { InitializeComponent(); this.redSlider.ValueChanged += slider_ValueChanged; this.greenSlider.ValueChanged += slider_ValueChanged; this.blueSlider.ValueChanged += slider_ValueChanged; } #endregion //////////////////////////////////////////////////////////////////////////////////////////////////// Method ////////////////////////////////////////////////////////////////////////////////////////// Private #region 슬라이더 값 변경시 처리하기 - slider_ValueChanged(sender, e) /// <summary> /// 슬라이더 값 변경시 처리하기 /// </summary> /// <param name="sender">이벤트 발생자</param> /// <param name="e">이벤트 인자</param> private void slider_ValueChanged(object sender, ValueChangedEventArgs e) { this.boxView.Color = Color.FromRgb(this.redSlider.Value, this.greenSlider.Value, this.blueSlider.Value); } #endregion } |