2010-03-29 3 views
0

버튼 중 하나에 대해 사용자 정의 IComand 클래스를 구현했습니다. 단추는 'MyPage.xaml'페이지에 있지만 사용자 정의 ICommand 클래스는 MyPage 코드 뒤에서가 아니라 다른 클래스에 배치됩니다. 그런 다음 XAML에서 나는 그것의 사용자 지정 명령 클래스와 버튼을 바인딩 할 다음 내가 할 :WPF 버튼을 통한 명령

MyPage.xaml :

<Page ...> 

     <Page.CommandBindings> 
      <CommandBinding Command="RemoveAllCommand" 
       CanExecute="CanExecute" 
       Executed="Execute" /> 
     </Page.CommandBindings> 

     <Page.InputBindings> 
       <MouseBinding Command="RemoveAllCommand" MouseAction="LeftClick" /> 
     </Page.InputBindings> 

      <...> 
       <Button x:Name="MyButton" Command="RemoveAllCommand" .../> 
      <...> 

    </Page> 

및 사용자 정의 명령 단추 클래스 :

// Here I derive from MyPage class because I want to access some objects from 
// Execute method 
public class RemoveAllCommand : MyPage, ICommand 
{ 

    public void Execute(Object parameter) 
    { 
     <...> 
    } 

    public bool CanExecute(Object parameter) 
    { 
     <...> 
    } 


    public event EventHandler CanExecuteChanged 
    { 
     add { CommandManager.RequerySuggested += value; } 
     remove { CommandManager.RequerySuggested -= value; } 
    } 

} 

내 문제 버튼에 대한 Execute 및 CanExecute 메소드가 다른 클래스에 있고 Button 뒤에있는 코드가 아닌 MyPage.xaml을 말하는 법이 있습니다. How to say 이러한 메서드는 XAML 페이지에서 RemoveAllCommand 클래스에 있습니다.

또한 마우스 이벤트가 단추에서 생성되어 입력 바인딩을 수행 할 때이 명령을 실행하고 싶습니다. 맞습니까?

감사

답변

1

당신이 ICommand의를 가지고 있기 때문에, 당신은 명령 속성을 통해 버튼에 바인딩 할 수 있으며이 활성화/CanExecute 전화 자체를 비활성화 할 때이 방법을 실행합니다 즉, 버튼을, 그것을 사용합니다 버튼을 누릅니다. 다른 입력 바인딩이 필요 없습니다.

이제 문제는 버튼이 명령을 찾아야한다는 것입니다. 간단한 해결책은 단추의 DataContext (또는 부모)에 명령의 인스턴스를 넣는 것입니다.

+0

안녕하세요, 성공했지만 프로젝트를 만들 때 오류가 발생했습니다. RemoveAllCommand 유형의 인스턴스를 만들 수 없습니다. 내 답변에 설명해 놓았습니다. – toni

0

이 이봐, 난이 있지만 그것을했을

<Button Command="{Binding RemoveAll}" .. /> 

과 CommandBinding을하고 InputBinding을 제거 : DataContext를 유형의 RemoveAllCommand의에서 removeAll라는 속성이있는 경우

, 당신은 단순히 당신의 XAML 버튼을 변경할 수 있습니다 성공하지 못했습니다. 프로젝트를 빌드 할 때 오류가 발생했습니다. RemoveAllCommand 유형의 인스턴스를 만들 수 없습니다.

나는 그것을했을 :

MyPage.xaml (I 페이지에서 CommandBinding을하고 InputBinding을 제거한) :

사용자 정의 ICommand의 클래스에서
<Page x:Class="GParts.Pages.MyPage" 
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
xmlns:Microsoft_Windows_Themes="clr-namespace:Microsoft.Windows.Themes; 
assembly=PresentationFramework.Aero"    
xmlns:Classes="clr-namespace:GParts.Classes" 
xmlns:Pages="clr-namespace:GParts.Pages"> 

<Page.Resources> 
     <...> 
     <Pages:RemoveAllCommand x:Key="RemoveAllCommandInstance"/> 
     <...> 
</Page.Resources> 

<...> 
<Button Command="{Binding RemoveAllCommandInstance}" ...> 
<...> 
</Page> 

나는 매개 변수없이 생성자를 추가 한 :

public class RemoveAllCommand : MyPage, ICommand 
{ 

    public RemoveAllCommand() { } 

    ... 
} 

감사합니다.

+1

여기서 문제는 {StaticResource RemoveAllCommandInstance}를 사용하여 명령을 가져와야한다는 것입니다. – Timores

0

오케이, 감사합니다. 내일 사용해 보겠습니다.

이제는 문제를 피하기 위해 RemoveAllCommandClass 클래스를 모두 MyPage의 코드 뒤에 옮기고 약간 수정했습니다.

는 1 .- 나는 MyPage.xaml이 추가 :

의 xmlns : 로컬 = "CLR-네임 스페이스 : GParts"나는 짓을

:

<Page.CommandBindings> 
    <CommandBinding Command="{x:Static local:Pages.MyPage._routedCommand}" 
       Executed="Execute" 
       CanExecute="CanExecute"/> 
</Page.CommandBindings> 


<Button Command="{x:Static local:Pages.MyPage._routedCommand}" .../> 

그리고 모두 OK입니다 작동합니다.버튼을 누르면 Execute 메서드에서 호출되는 백그라운드 작업자 (bw)가 실행됩니다. bw는 다른 클래스에 속합니다. 백그라운드 작업자 클래스에는 bw가 실행 중인지 나타내는 변수 (isRunning)가 있습니다. DoWork 이벤트를 실행하기 전에이를 true로 설정하고 bw가 완료되면 RunWorkerCompleted에서 false로 설정합니다. 그래서 CanExecute에서 bw 클래스의 isRunning을 확인하고 isRunning이 false이면 e.canExecute를 true로 설정하고 isRunning이 true이면 e.canExecute를 false로 설정합니다. 따라서 bw가 실행 중일 때 WPF에 의해 버튼이 자동으로 비활성화되지만 bw가 끝나면 버튼은 계속 비활성화되고 다시 누를 때까지 활성화되지 않습니다. bw가 끝나면 다시 버튼을 누를 때까지 WPF가 버튼 상태를 enabled로 업데이트하지 않는 이유는 무엇입니까?

감사합니다.

+0

WPF에게 명령 상태가 변경되었음을 알려야합니다. 이 목적을 위해 CanExecuteChanged 이벤트가 있습니다. – Timores