보통 control을 만들때 genneric.xaml에 디자인을 구현하고 x:Name을 이용해서 해당 control의 cs안에서 컨트롤 하고 싶을때
이렇게 하면 된다.
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Windows; using System.Windows.Controls; using System.Windows.Media; using Microsoft.Surface.Presentation.Controls; namespace MyControl { [TemplatePart(Name = SurfaceInkCanvasName, Type = typeof(SurfaceInkCanvas))] public class DrawPanelControl : Control { //template internal const string SurfaceInkCanvasName = "xSurfaceInkCanvas"; internal SurfaceInkCanvas SurfaceInkCanvasContainer; public DrawPanelControl() { DefaultStyleKey = typeof(DrawPanelControl); } public override void OnApplyTemplate() { SurfaceInkCanvasContainer = this.GetTemplateChild(SurfaceInkCanvasName) as SurfaceInkCanvas; SurfaceInkCanvasContainer.DefaultDrawingAttributes.Height = 10; SurfaceInkCanvasContainer.DefaultDrawingAttributes.Width = 10; SurfaceInkCanvasContainer.DefaultDrawingAttributes.Color = Colors.Red; base.OnApplyTemplate(); } } }
저렇게 TemplatePart를 class밖에서 잡고 OnApplyTemplate에서
SurfaceInkCanvasContainer = this.GetTemplateChild(SurfaceInkCanvasName) as SurfaceInkCanvas;
이렇게 정의 하면 된다.
generic.xaml에는 그냥 일반적으로 x:name을 썻다.
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:controls="clr-namespace:MyControl" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:Custom="http://schemas.microsoft.com/surface/2008" xmlns:sf="clr-namespace:Microsoft.Surface.Presentation.Controls;assembly=Microsoft.Surface.Presentation" mc:Ignorable="d"> <Style TargetType="{x:Type controls:DrawPanelControl}"> <Setter Property="Template"> <Setter.Value> <ControlTemplate TargetType="{x:Type controls:DrawPanelControl}"> <Grid> <sf:SurfaceInkCanvas x:Name="xSurfaceInkCanvas" Background="Bisque"/> </Grid> </ControlTemplate> </Setter.Value> </Setter> </Style> </ResourceDictionary>
'Surface' 카테고리의 다른 글
[surface] 해당 UserControl만 스크린샷(캡쳐) 하기 (0) | 2012.04.26 |
---|---|
[surface] Media.Brush를 Media.Color로 Convert 하는 법 (0) | 2012.04.25 |
[surface] Control만들때 OnApplyTemplate()이 호출되지 않을때 (0) | 2012.04.25 |
[surface] 로봇캅 폴리 포스팅 퍼오기 ㅋ (0) | 2011.09.14 |
[surface] surface 런칭쇼에 내가 만든 프로그램 공개 (0) | 2011.05.27 |