2019년 1월 26일 토요일

(WPF교육)

WPF 윈도우 Content속성, n 사용자의 입력을 Window의 Content로 지정하는 예제를 작성해 보자.
WPF 응용프로그램으로 프로젝트 생성 후 MainWindow.xaml.cs 파일을 수정하자.
MainWindow.xaml.cs
using System.Windows;
using System.Windows.Input;
using System.Text;
namespace ContentTest2
{
public partial class MainWindow : Window
{
StringBuilder s;
public MainWindow()
{
InitializeComponent();
Title = "사용자 입력을 Window의 Content 속성에 매핑하기";
s = new StringBuilder("");
Content = s;
}
//키가 눌릴때 마다 Content 프로퍼티가 변경되고 Window 클래스는 여기에 반응해서
//클라이언트 영역을 다시 그린다.
protected override void OnTextInput(TextCompositionEventArgs args)
{
string str = Content.ToString();
if (args.Text == "\b")
{
if (str.Length > 0)
s.Remove(s.Length - 1, 1);
//스트링 변수 s 인경우
// s = s.Remove(s.Length - 1, 1);
}
else
{
//스트링 변수 s 인경우
// s += args.Text;
s.Append(args.Text);
}
// s를 string을 선언하면 null로 설정하지 않아도 된다.
// string 클래스는 문자열을 추가하면 새로 메모리 allocation을 한다.
// StringBuilder는 문자열이 추가되는 경우 기존의 문자열 뒤에 추가를 한다.
Content = null;
Content = s;
}
}
}
n 실행결과
예제에서 Content에 문자열을 입력했지만 진정한 목적은 UIElement를 상속받은 어떠한 요소라도 입력이 가능하다는 것이다. UIElement는 WPF에서 중요한 것으로 키보드, 마우스, 스타일러스 펜을 처리하는 것이 구현되어 있다. Content 프로퍼티의 동작과 관련하여 두 가지로 구분되는데 UIElement를 상속받은 것과 아닌 것으로 구별할 수 있다. 전자는 OnRender()로 출력해야 하고 후자는 ToString() 메소드로 객체를 출력해야 한다.

댓글 없음:

댓글 쓰기

(C#교육동영상)C# ADO.NET 실습 ODP.NET/ODAC 설치 오라클 함수 호출 실습, C#학원, WPF학원, 닷넷학원, 자바학원

  (C#교육동영상)C# ADO.NET 실습  ODP.NET/ODAC 설치  오라클 함수 호출 실습, C#학원, WPF학원, 닷넷학원, 자바학원 https://www.youtube.com/watch?v=qIPU85yAlzc&list=PLxU-i...