■ Viewbox 엘리먼트의 Stretch/StretchDirection 속성을 사용하는 방법을 보여준다.
▶ MainWindow.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 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 |
<?xml version="1.0" encoding="utf-8"?> <Window x:Class="TestProject.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Title="TestProject"> <Grid Margin="10"> <Grid.ColumnDefinitions> <ColumnDefinition Width="*" /> <ColumnDefinition Width="*" /> </Grid.ColumnDefinitions> <Viewbox Name="viewbox" Grid.Column="0" HorizontalAlignment="Center" VerticalAlignment="Center" Width="{x:Bind sizeSlider.Value, Mode=OneWay}" Height="{x:Bind sizeSlider.Value, Mode=OneWay}" Stretch="Uniform" StretchDirection="Both"> <Border BorderThickness="15" BorderBrush="Gray"> <StackPanel Background="DarkGray"> <StackPanel Orientation="Horizontal"> <Rectangle Width="40" Height="10" Fill="Blue" /> <Rectangle Width="40" Height="10" Fill="Green" /> <Rectangle Width="40" Height="10" Fill="Red" /> <Rectangle Width="40" Height="10" Fill="Yellow" /> </StackPanel> <Image Source="ms-appx:///IMAGE/Slices.png" /> <TextBlock HorizontalAlignment="Center" Text="This is text." /> </StackPanel> </Border> </Viewbox> <StackPanel Grid.Column="1" HorizontalAlignment="Center" VerticalAlignment="Center" Width="200"> <Slider Name="sizeSlider" Header="Width/Height" Minimum="20" Maximum="300" Value="200" /> <RadioButtons Header="Stretch"> <RadioButton Name="noneStretchRadioButton" Tag="None" Content="None" /> <RadioButton Name="fillStretchRadioButton" Tag="Fill" Content="Fill" /> <RadioButton Name="uniformStretchRadioButton" Tag="Uniform" Content="Uniform" /> <RadioButton Name="uniformToFillStretchRadioButton" Tag="UniformToFill" Content="UniformToFill" /> </RadioButtons> <RadioButtons Header="StretchDirection"> <RadioButton Name="upOnlystretchDirectionRadioButton" GroupName="StretchDirection" Tag="UpOnly" Content="UpOnly" /> <RadioButton Name="downOnlystretchDirectionRadioButton" GroupName="StretchDirection" Tag="DownOnly" Content="DownOnly" /> <RadioButton Name="bothstretchDirectionRadioButton" GroupName="StretchDirection" Tag="Both" Content="Both" /> </RadioButtons> </StackPanel> </Grid> </Window> |
▶ MainWindow.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 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 |
using Microsoft.UI.Xaml; using Microsoft.UI.Xaml.Controls; using Microsoft.UI.Xaml.Media; namespace TestProject { /// <summary> /// 메인 윈도우 /// </summary> public sealed partial class MainWindow : Window { //////////////////////////////////////////////////////////////////////////////////////////////////// Constructor ////////////////////////////////////////////////////////////////////////////////////////// Public #region 생성자 - MainWindow() /// <summary> /// 생성자 /// </summary> public MainWindow() { InitializeComponent(); this.noneStretchRadioButton.Checked += stretchRadioButton_Checked; this.fillStretchRadioButton.Checked += stretchRadioButton_Checked; this.uniformStretchRadioButton.Checked += stretchRadioButton_Checked; this.uniformToFillStretchRadioButton.Checked += stretchRadioButton_Checked; this.upOnlystretchDirectionRadioButton.Checked += stretchDirectionRadioButton_Checked; this.downOnlystretchDirectionRadioButton.Checked += stretchDirectionRadioButton_Checked; this.bothstretchDirectionRadioButton.Checked += stretchDirectionRadioButton_Checked; this.uniformStretchRadioButton.IsChecked = true; this.bothstretchDirectionRadioButton.IsChecked = true; } #endregion //////////////////////////////////////////////////////////////////////////////////////////////////// Method ////////////////////////////////////////////////////////////////////////////////////////// Private #region Stretch 라디오 버튼 체크시 처리하기 - stretchRadioButton_Checked(sender, e) /// <summary> /// Stretch 라디오 버튼 체크시 처리하기 /// </summary> /// <param name="sender">이벤트 발생자</param> /// <param name="e">이벤트 인자</param> private void stretchRadioButton_Checked(object sender, RoutedEventArgs e) { if(sender is RadioButton radioButton && this.viewbox != null) { string stretch = radioButton.Tag.ToString(); switch(stretch) { case "None" : this.viewbox.Stretch = Stretch.None; break; case "Fill" : this.viewbox.Stretch = Stretch.Fill; break; case "Uniform" : this.viewbox.Stretch = Stretch.Uniform; break; case "UniformToFill" : this.viewbox.Stretch = Stretch.UniformToFill; break; } } } #endregion #region StretchDirection 라디오 버튼 체크시 처리하기 - stretchDirectionRadioButton_Checked(sender, e) /// <summary> /// StretchDirection 라디오 버튼 체크시 처리하기 /// </summary> /// <param name="sender">이벤트 발생자</param> /// <param name="e">이벤트 인자</param> private void stretchDirectionRadioButton_Checked(object sender, RoutedEventArgs e) { if(sender is RadioButton radioButton && this.viewbox != null) { string stretchDirection = radioButton.Tag.ToString(); switch(stretchDirection) { case "UpOnly" : this.viewbox.StretchDirection = StretchDirection.UpOnly; break; case "DownOnly" : this.viewbox.StretchDirection = StretchDirection.DownOnly; break; case "Both" : this.viewbox.StretchDirection = StretchDirection.Both; break; } } } #endregion } } |