[WPF, Silverlight] Behind Code에서 Binding 구현하기
[Binding 설정하기]
TestModel model = new TestModel(); // Binding에 필요한 Source
TextBox textBox= new TextBox (); // Binding할 Control 생성
Binding binding = new Binding(); // Binding 객체를 생성
binding.Source = model ; // Binding 객체에 Source를 설정
binding.Path = new PropertyPath("Title"); // Source 에서 Binding 에 사용할 Property 설정
binding.Mode = BindingMode.TwoWay; // Binding Mode를 설정한다.
BindingOperations.SetBinding (textBox , TextBox.TextProperty, binding); // textBox의 Binding할 Property를 설정하고 Binding 객체를 설정 한다.
[Binding Mode]
속성 |
설명 |
TwoWay |
대상 속성이나 소스 속성 중 하나가 변경될 때마다 대상 속성이나 소스 속성을 업데이트 |
OneWay |
소스 속성이 변경될 때 대상 속성을 업데이트 |
OneTime |
응용 프로그램이 시작되거나 DataContext가 변경될 때만 대상 속성을 업데이트 |
OneWayToSource |
대상 속성이 변경될 때 소스 속성을 업데이트 |
Default |
대상 속성의 기본 Mode 값을 사용 |
[Binding 모두 제거하기]
BindingOperations.ClearBinding(control, Control.TitleProperty); // control에 설정되어 있는 Title Binding을 제거
[Binding 모두 제거하기]
BindingOperations.ClearAllBindings(control); // control에 설정되어 있는 Binding을 모두 제거
MSDN : http://msdn.microsoft.com/ko-kr/library/ms617928(v=vs.90).aspx