2010-06-22 4 views
0

컬렉션을 treeveiw에 바인딩 할 수 있지만 wpc 제어를위한 하나의 simle 객체를 핫 바인드 할 수는 없습니다.WPF 기본 바인딩 하나의 객체

<UserControl x:Class="ReporterWpf.UserControl1" 
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
    Height="300" Width="300"> 
    <Grid> 
     <StackPanel> 
      <TextBox Name="{Binding Path=Name}"></TextBox> 
      <TextBox Name="{Binding Path=Age}"></TextBox> 
     </StackPanel> 
    </Grid> 
</UserControl> 


public Person 
{ 
public string Name {get;set;} 
public int Age {get;set;} 
} 

public partial class UserControl1 : UserControl 
    { 
     public UserControl1() 
     { 
      InitializeComponent(); 
     } 
public UserControl1(Person person):this() 
     { 
     Person person=new Person(); 
person.Age=19; 
person.Name = "Patrick" 
     } 
    } 

이 두 속성을 바인딩하는 마법의 줄은 무엇입니까?

답변

4

부모 요소 중 하나의 DataContext을 설정해야합니다. 예를 들어

:

this.DataContext = person; 

두 개의 다른 패널에 두 사람이 결합 할 경우, 별도로 각 패널의 DataContext를 설정해야합니다. expresions 결합했다 당신은 단지 부모 컨테이너에 데이터 컨텍스트를 줄 필요

+0

+1 .. 이것은 가장 간단한 방법이지만 DataContext를 설정하기위한 몇 가지 옵션이 있습니다. 폴 스토벨 (Paul Stovell)은 여기에서 옵션을 탐구했다 : http://www.paulstovell.com/mvvm-instantiation-approaches – kiwipom

0

(또는 사람들을 보유하고 부모 개체에 모두 결합) :

this.DataContext= person; 

"사람"예를 클래스의를