■ LayoutInformation 클래스의 GetLayoutSlot 정적 메소드를 사용해 특정 엘리먼트 경계 상자를 구하고 해당 윤곽선을 표시하는 방법을 보여준다.
▶ 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 |
<Window x:Class="TestProject.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Width="800" Height="600" Title="TestProject" FontFamily="나눔고딕코딩" FontSize="16"> <Grid Name="grid" HorizontalAlignment="Center" VerticalAlignment="Center" Margin="10" Background="LightSteelBlue"> <Grid.ColumnDefinitions> <ColumnDefinition Width="Auto" /> </Grid.ColumnDefinitions> <Grid.RowDefinitions> <RowDefinition Height="Auto" /> <RowDefinition Height="Auto" /> <RowDefinition Height="Auto" /> </Grid.RowDefinitions> <TextBlock Name="textBlock1" Grid.Row="0" Margin="10"> Hello World! </TextBlock> <Button Name="showBoundingBoxButton" Grid.Row="1" Margin="10" Width="150" Height="30"> Show Bounding Box </Button> <TextBlock Name="textBlock2" Grid.Row="2" Margin="10" /> </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 |
using System.Windows; using System.Windows.Controls; using System.Windows.Controls.Primitives; using System.Windows.Media; using System.Windows.Shapes; namespace TestProject { /// <summary> /// 메인 윈도우 /// </summary> public partial class MainWindow : Window { //////////////////////////////////////////////////////////////////////////////////////////////////// Constructor ////////////////////////////////////////////////////////////////////////////////////////// Public #region 생성자 - MainWindow() /// <summary> /// 생성자 /// </summary> public MainWindow() { InitializeComponent(); this.showBoundingBoxButton.Click += showBoundingBoxButton_Click; } #endregion //////////////////////////////////////////////////////////////////////////////////////////////////// Method ////////////////////////////////////////////////////////////////////////////////////////// Private #region Show Bounding Box 버튼 클릭시 처리하기 - showBoundingBoxButton_Click(sender, e) /// <summary> /// Show Bounding Box 버튼 클릭시 처리하기 /// </summary> /// <param name="sender">이벤트 발생자</param> /// <param name="e">이벤트 인자</param> private void showBoundingBoxButton_Click(object sender, RoutedEventArgs e) { RectangleGeometry rectangleGeometry = new RectangleGeometry(); rectangleGeometry.Rect = LayoutInformation.GetLayoutSlot(this.textBlock1); Path path = new Path(); path.StrokeThickness = 5; path.Stroke = Brushes.LightGoldenrodYellow; path.Data = rectangleGeometry; Grid.SetColumn(path, 0); Grid.SetRow (path, 0); this.grid.Children.Add(path); this.textBlock2.Text = $"LayoutSlot is equal to {LayoutInformation.GetLayoutSlot(this.textBlock1)}"; } #endregion } } |