캔바스 크기에 따라서 캔바스 안에 컨트롤을 중앙에 위치하도록 해보자
실행된 모습
<Window xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:my="clr-namespace:Com.Mariner.ERP.TestWPF" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" mc:Ignorable="d" x:Class="Com.Mariner.ERP.TestWPF.MainWindow" Title="MainWindow" Height="405" Width="669" Loaded="Window_Loaded"> <Grid > <Grid.RowDefinitions> <RowDefinition/> <RowDefinition Height="40.003"/> </Grid.RowDefinitions> <Grid.Background> <LinearGradientBrush EndPoint="0.5,1" MappingMode="RelativeToBoundingBox" StartPoint="0.5,0"> <GradientStop Color="#FF373737" Offset="1"/> <GradientStop Color="#FF878787"/> </LinearGradientBrush> </Grid.Background> <Canvas ScrollViewer.HorizontalScrollBarVisibility="Auto" ScrollViewer.VerticalScrollBarVisibility="Auto" d:LayoutOverrides="Width" Name="CanvasMain"> <Canvas.Background> <LinearGradientBrush EndPoint="0.5,1" StartPoint="0.5,0"> <GradientStop Color="#FFCACACA" Offset="0"/> <GradientStop Color="#FF6A6A6A" Offset="1"/> </LinearGradientBrush> </Canvas.Background> <Rectangle Fill="#FFF4F4F5" Height="137" Canvas.Left="8" Stroke="Black" Width="266" Canvas.Top="6" Name="recItem" /> </Canvas> <Button Content="check Size" HorizontalAlignment="Right" Margin="0,7.997,8,4.003" Width="123" Click="Button_Click" Grid.Row="1" /> <TextBox Height="28" HorizontalAlignment="Left" Margin="8,8,0,0" Name="textBox1" VerticalAlignment="Top" Width="492" Grid.Row="1" /> </Grid> </Window>
CanvasMain.RenderSize //캔버스의 렌더링된 사이즈를 가져올수 있다.
비하인드 코드
private void Button_Click(object sender, RoutedEventArgs e) { SetSize(this.CanvasMain.RenderSize); //버튼클릭했을때 값을 확인해보자 } private void SetSize(Size size) { double winl = (size.Width - this.recItem.ActualWidth) / 2; //가로크기에서 캔바스안에 아이템 넓이를 뺐따 double wint = (size.Height - this.recItem.ActualHeight) / 2;//가로크기에서 캔바스안에 아이템 높이를 뺐따 // Set new position of object. recItem.SetValue(Canvas.TopProperty, wint); recItem.SetValue(Canvas.LeftProperty, winl); this.textBox1.Text = string.Format("Width: {0} Height: {1}", size.Width, size.Height); }
'WPF' 카테고리의 다른 글
[DependencyObject.GetValue] 사용하기 (0) | 2010.09.16 |
---|---|
[Blend4 / DataGrid / DataTemplate / CellEditingTemplate / Edit] 데이터그리드 편집모드 사용하기 (0) | 2010.09.16 |
StaticResource 와 DynamicResource 의 차이 (0) | 2010.09.15 |
[StringFormat] 초간단 날짜 표시방법변경 (0) | 2010.09.01 |
[NavigationService / PageFunction] 페이지 이동 (2) | 2010.08.23 |
[DataGridHyperlinkColumn , EventSetter ] DataGridHyperlink 이벤트 처리 (0) | 2010.08.03 |
[WPF ReportViewer] WPF WinForm 리포트뷰 사용하기 (0) | 2010.08.02 |
[WPF Style/FindResource ] 비하인드 코드에서 스타일 먹히기 (0) | 2010.07.30 |
[WPF FileDialog] Image 저장/불러오기/바인딩하기 (0) | 2010.07.19 |
[MSDN]WPF/Silverlight데이터 바인딩 방법 항목 (0) | 2010.07.15 |