2011-04-30 3 views
0

두 개의 단추가있는 3.5 WPF 사용자 정의 컨트롤이 있고 사용자 정의 컨트롤의 xaml을 통해이 단추에 명령을 바인딩 할 수 있기를 바랍니다.WPF 사용자 컨트롤 XAML 명령

x:Usercontrol x:Name:fou Button1Command="{Binding StuffCommandHandler}" Button2Command="{Binding Stuff2CommandHandler}" 

위의 바인딩이 작동하지 않는 문제가 있습니다. xaml을 통해 사용자 정의 컨트롤의 단추 중 두 개에 명령을 바인딩하는 방법은 무엇입니까?

나는 뒤에있는 UserControl의 코드에서이있어 그리고 당신은 종속성 속성에 Button1CommandHandler을해야

private ICommand _button1Command; 
    public ICommand Button1CommandHandler 
    { 
     get { return _button1Command; } 
     set { _button1Command = value; } 
    } 

답변

3

을 Button1.Command하는 Button1CommandHandler 바인딩 :

public static readonly DependencyProperty ButtonCommandProperty = 
    DependencyProperty.Register("ButtonCommand", typeof(ICommand), typeof(TwoButtons), new PropertyMetadata(default(ICommand))); 

public ICommand ButtonCommand 
{ 
    get { return (ICommand)GetValue(ButtonCommandProperty); } 
    set { SetValue(ButtonCommandProperty, value); } 
} 

다음이 결합하여 버튼의 Command. 당신이 코드에서 버튼을 만드는 경우는 다음과 같이 바인딩 할 수 있습니다 더 나은 작업

button.SetBinding(Button.CommandProperty, new Binding("ButtonCommand") { Source = this }); 
+0

, Button1.Command = ButtonCommand하지만 컨트롤이 이해할 수있는 생성 될 때 ButtonCommand은 null입니다. 그러나 xaml 바인딩이 발생하면 Buttoncommand 세트가 시작되지 않으므로 hte 버튼을 클릭하면 StuffCommandHandler가 실행되지 않습니다. 내가 뭘 놓치고 있니? – mike

+0

ButtonCommand 세트의 중단 점을 넣었습니다. 결코 실행되지 않습니다. – mike

+0

* Button1.Command를 * ButtonCommand에 바인드해야합니다. – svick