반응형
MainWindow.xaml
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
<Window x:Class="STEP1.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:STEP1" mc:Ignorable="d" Title="MainWindow" Height="233.872" Width="237.096" WindowStyle="ThreeDBorderWindow"> <Canvas> <Button x:Name="button" Content="Button" Canvas.Left="77" Canvas.Top="150" Width="75" Click="button_Click"/> <TextBox x:Name="textBox" Height="24" Canvas.Left="64" TextWrapping="Wrap" Text= "{Binding Message}" Canvas.Top="66" Width="98" RenderTransformOrigin="0.344,0.893"/> </Canvas> </Window> |
레이아웃으로 Canvas를 사용
Canvas의 기본 Height 및 Width 속성은 0이지만, 자식 요소의 크기를 자동으로 지정하는 요소의 자식일 때 예외입니다.
Canvas의 자식 요소는 크기가 조정되지 않으며 지정된 좌표에 배치되기만 합니다.
기본적인 크기 조정 제약 조건이나 맞춤을 적용할 필요가 없거나 적용하지 않으려는 경우 이러한 기능을 유연하게 활용할 수 있습니다.
Canvas의 자식 요소에는 항상 요청되는 전체크기가 할당되며,
Canvas내에서는 가로 및 세로 맞춤이 적용되지 않습니다.
이 말은 Canvas크기가 달라지더라도 Canvas안의 요소의 크기는 변하지 않는다는 것입니다.
Text= "{Binding Message}" MainWindow.cs의 Message속성과 참조 되도록 설정하는 부분이라고 한다.
MainWindow.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 |
using System.Windows.Controls; using System.Windows.Data; using System.Windows.Documents; using System.Windows.Input; using System.Windows.Media; using System.Windows.Media.Imaging; using System.Windows.Navigation; using System.Windows.Shapes; namespace STEP1 { /// <summary> /// MainWindow.xaml에 대한 상호 작용 논리 /// </summary> public partial class MainWindow : Window { private string _Message = string.Empty; public string Message { get { return this._Message;} set { this._Message = value; } } public MainWindow() { InitializeComponent(); this.DataContext = this; } private void button_Click(object sender, RoutedEventArgs e) { this.Title = this.Message; } } } |
위에서 말한 Message는 TextBox의 Text에 바인딩 된 속성이며 바인딩 되는 속성은 public이어야 한다.
this.DataContext = this;
MainWindow.xaml에서 바인딩한 소스 검색을 MainWindow에서 하도록 지정 한다.
xaml에서 바인딩한 Message 속성은 MainWindow 클래스에서 찾는다.
실행화면