WPF 데이터바인딩, ListBox와 Linq 쿼리를 이용한 간단한 데이터바인딩, 새창 띄우기, 이벤트 및 델리게이트를 통한 메인 윈도우의 ListBox Refresh 적용 실습
n 직무타입(내근:Inner, 외근:OutSide)과 직무목록을 보여주는 두개의 ListBox 컨트롤을 사용해서 직무타입을 선택하면 Linq를 통해 해당 직무타입의 직무를 쿼리해서 하단의 ListBox에 보여주는 예제이다.
n “직무추가” 버튼을 클릭하면 새창이 뜨고 직무를 입력 후 저장 버튼을 클릭하면 직무입력 화면이 사라지고 메인 윈도우 상단의 ListBox가 새창에서 입력한 직무타입으로 선택되면서 하단의 ListBox는 해당 직무타입의 직무목록으로 자동으로 Refresh 된다.
n WPF 프로젝트를 생성하자. (본 예제에서는 프로젝트명을 “WpfApp1”로 설정했다.)
n [Duty.cs]
n [MainWindow.xaml]
n [MainWindow.xaml.cs]
n [SubWindow.xaml]
n [SubWindow.xaml.cs]
Delegate vs delegate
1. The delegate keyword is for the compiler to do some magic for you. When you declare a new delegate with a custom signature,
- the compiler creates a new Type for you derived from MulticastDelegate (which in turn derives from Delegate).
- the compiler adds an Invoke method with your custom signature
- similarly the compiler adds BeginInvoke and EndInvoke methods for this new type
So now when you call delObject(args) - the compiler translates that to delObject.Invoke(args)
2. The Delegate base class provides some functionality such as
- CreateDelegate (for obtaining a delegate wrapping a static/instance method)
- DynamicInvoke (to invoke a delegate with a list of arguments - late bound)
- Combine and Remove (for delegate chaining.. chain multiple delegates together e.g. multiple event handler delegates for an event)
The C# compiler forbids you from deriving from Delegate explcitly in your code.. you have to use the delegate keyword.