2012-10-10 3 views
0

오늘 오후에 뭔가를 구현하려고했지만 원하는대로 작동하지 못했습니다. 나는 다음과 같은 몇 가지 XAML이있는 경우사용자 컨트롤에 의해 노출 된 명령에 바인딩

나는 (나는 그것이 완전하지 알고 있지만 그것은 단지 설명을위한 것입니다) ...

<Window> 
    <StackPanel> 
    <Button /> 
    <UserControl1> 
    </StackPanel> 
</Window> 

을 ... 그리고 UserControl1을 현재 (명령 속성을 노출 RelayCommand를 사용하고 있습니다.),이 명령에서 Window의 Button을 어떻게 바인딩합니까? 내가 돌아서 수 및 다시 바인드 단추에이 바인딩 할 수 있지만 UserControl1 명령 속성에 바인딩 된 창에서 속성을 노출 할했습니다 있지만 창 속성은 항상 null입니다. 이 패턴은 다른 속성 (정수 값)에서 작동하는 것 같습니다. 이 작업을 수행하는 더 좋은 방법이 있습니까? 이 문제를 방지하는 명령이 있습니까?

답변

1

(1) 명령을 DependedecyProperty로 정의하고 구현하십시오.

(2) 단추의 Command 속성을 UserControl의 MyCommand 속성에 바인딩하십시오.

public Class UserControl1 : UserControl 
{ 

     public UserControl1() 
     { 
      MyCommad = new RelayCommand 
        (
         () => { // do some stuff in execute delegate}, 
         () => { return true ;}               
        ); 
     } 

     public bool MyCommand 
     { 
      get { return (ICommand)GetValue(MyCommandProperty); } 
      set { SetValue(MyCommandProperty, value); } 
     }  

     public static readonly DependencyProperty MyCommandProperty = 
          DependencyProperty.Register("MyCommand", typeof(ICommand),new FrameworkPropertyMetadata(null)); 

} 

XAML :

의미가
<Window> 
     <StackPanel> 
      <Button Command={Binding ElementName=control1,Path=MyCommand,Mode=OneWay/> 
      <UserControl1 x:Name="control1" /> 
     </StackPanel> 
</Window> 
+0

. 내가 윈도우의 뷰 모델에서 사용자 정의 컨트롤에 의해 드러난 커맨드를 어떻게 다시 바인딩 할 것인가? –

+0

각 사용자 정의 컨트롤의 명령을 두 개 이상의 장소에 바인딩해야합니까? –

관련 문제