2011-11-18 3 views
0

나는 동적 요소를 displaed 요소로 변경할 수있는 사용자 지정 개체를 바인딩하려고합니다.동적 개체 바인딩 wpf

내 window.xaml 이제이 있습니다 window.xaml.cs에서

<StackPanel Height="310" HorizontalAlignment="Left" Margin="12,12,0,0" Name="Configuration_stackPanel" VerticalAlignment="Top" Width="264" Grid.Column="1"> 
<Label Content="{Binding Path=Client}" Height="22" HorizontalAlignment="Left" Margin="20,0,0,0" Name="Client" VerticalAlignment="Top" /> 
</StackPanel> 

, 난

public CustomObject B; 

CustomObject가 클라이언트 멤버가있다 회원을 가지고있다. B.Client는 다른 것 중에서 클라이언트 이름 (문자열)을 가져옵니다.

B.Client를 표시하고 코드가 변경되면 어떻게해야합니까?

예 : 코드에서 B.Client = "foo"를 실행하면 foo가 으로 표시되고 B.Client = "bar"로 설정하면 foo 대신 bar가 표시됩니다. 사전에

감사
F

+0

당신이하십시오 CustomObject의 정의를 제공 할 수 있습니까? –

답변

2

귀하의 CustomObject 클래스는 INotifyPropertyChanged 인터페이스를 구현해야합니다

public class CustomObject : INotifyPropertyChanged 
{ 

    private string _client; 
    public string Client 
    { 
     get { return _client; } 
     set 
     { 
      _client = value; 
      OnPropertyChanged("Client"); 
     } 
    } 

    public event PropertyChangedEventHandler PropertyChanged; 

    protected virtual void OnPropertyChanged(string propertyName) 
    { 
     var handler = PropertyChanged; 
     if (handler != null) 
     handler(this, new PropertyChangedEventArgs(propertyName)); 
    } 

} 
+0

클라이언트를 찾을 위치를 xaml에 추가해야합니까? – djfoxmccloud

+0

속성에 바인딩하면됩니다. 바인딩이 변경되면 값이 자동으로 업데이트됩니다. –

+0

이것이 작동하지 않는 이유는 내가 묻는 이유입니다. StaticResource 또는 뭔가 다른 xaml 같은 추가해야합니까? – djfoxmccloud