728x90
반응형
728x170
▶ MainWindow.xaml
<Window x:Class="TestProject.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Width="600"
Height="450"
Title="컨트롤 템플리트에서 컨트롤 객체 구하기">
<Window.Resources>
<ControlTemplate x:Key="ButtonControlTemplateKey" TargetType="Button">
<Grid>
<Rectangle x:Name="rectangle"
Stroke="Black"
StrokeThickness="3"
RadiusX="10"
RadiusY="10"
Fill="Orange">
</Rectangle>
<Viewbox
Margin="{TemplateBinding Padding}"
VerticalAlignment="Center"
HorizontalAlignment="Center">
<ContentPresenter />
</Viewbox>
</Grid>
</ControlTemplate>
</Window.Resources>
<Grid>
<Button
Name="button"
HorizontalAlignment="Center"
VerticalAlignment="Center"
Width="280"
Height="80"
Template="{StaticResource ButtonControlTemplateKey}"
Padding="10"
Content="ChangeColor"
Click="button_Click" />
</Grid>
</Window>
728x90
▶ MainWindow.xaml.cs
using System;
using System.Windows;
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();
}
#endregion
//////////////////////////////////////////////////////////////////////////////////////////////////// Method
////////////////////////////////////////////////////////////////////////////////////////// Private
#region 버튼 클릭시 처리하기 - button_Click(sender, e)
/// <summary>
/// 버튼 클릭시 처리하기
/// </summary>
/// <param name="sender">이벤트 발생자</param>
/// <param name="e">이벤트 인자</param>
private void button_Click(object sender, RoutedEventArgs e)
{
try
{
Rectangle rectangle = this.button.Template.FindName("rectangle", button) as Rectangle;
rectangle.Fill = Brushes.Red;
}
catch(Exception exception)
{
MessageBox.Show(exception.Message);
}
}
#endregion
}
}
※ 테스트 결과 컨트롤이 로드되기 전에는 참조를 구할 수 없었다.
728x90
반응형
그리드형(광고전용)
'C# > WPF' 카테고리의 다른 글
[C#/WPF] Trigger 엘리먼트 : Property 속성을 사용해 컨트롤 템플리트 트리거에서 해당 엘리먼트에서 정의한 속성 설정하기 (0) | 2015.09.18 |
---|---|
[C#/WPF] DataTemplate 엘리먼트 : Triggers 속성 사용하기 (0) | 2015.09.18 |
[C#/WPF] Binding 태그 확장 : RelativeSource 속성에서 RelativeSource 태그 확장의 AncestorType 속성을 사용해 부모 엘리먼트 속성 바인딩하기 (0) | 2015.09.18 |
[C#/WPF] DataTemplate 엘리먼트 : 대상 엘리먼트 속성 바인딩 하기 (0) | 2015.09.18 |
[C#/WPF] ToolBar 클래스 : 오버플로우 패널 숨기기 (0) | 2015.09.17 |
[C#/WPF] Point 구조체 : 회전하기 (0) | 2015.09.14 |
[C#/WPF] Viewbox 엘리먼트 사용하기 (0) | 2015.09.10 |
[C#/WPF] 문자열 배열 리소스 사용하기 (0) | 2015.09.05 |
[C#/WPF] TextBox 클래스 : PreviewTextInput 이벤트를 사용해 숫자 입력하기 (0) | 2015.09.02 |
[C#/WPF] TextBox 클래스 : TextChanged 이벤트를 사용해 숫자 입력하기 (0) | 2015.09.02 |
댓글을 달아 주세요