2012-03-23 5 views
0

"부착 된 동작"에 대해 배우려고하고 있으며 약간의 문제가 있습니다. 현재 사용중인 문서는 다음과 같습니다. http://marlongrech.wordpress.com/2008/12/13/attachedcommandbehavior-v2-aka-acb/작동하도록 첨부 된 동작 받기

간단히 말해서 TreeView의 항목을 "클릭"하여 메모장에서 파일을 가져올 수 있기를 바랍니다. 메모장에서 WPF의 명령 단추를 통해 파일을 가져 오는이 코드가 있습니다. 지금은이 명령 버튼을 호출 해 봅시다 ... 테스트. 이 단추를보기에 바인딩하는 데 사용하는 XAML은 다음과 같습니다.

<TreeView ItemsSource="{Binding Courses}"> 
    <TreeView.ItemContainerStyle> 
    <Style TargetType="{x:Type TreeViewItem}"> 
     <Setter Property="local:CommandBehavior.Event" Value="MouseDoubleClick"/> 
     <Setter Property="local:CommandBehavior.Action" Value="{Binding Path=TestIng}"/> 
     <Setter Property="local:CommandBehavior.CommandParameter" Value="ShowThis" /> 
    </Style> 
    </TreeView.ItemContainerStyle> 
</TreeView> 

나는 트 리뷰의 항목을 더블 클릭하면 그것은 참으로 코드를 실행 않습니다

strategy.Execute (CommandParameter)

: "테스트"하지만, 다음 줄에 폭탄

실제 오류는 다음과 같습니다

개체 참조는

그래서이 나는 아마도 "CommandPa 설정되지 않았 음을 알려줍니다 개체의 intance로 설정되어 있지 므 라머 ".

"TestIng"명령 단추의 정의가있는 모델은 다음과 같습니다.

using System.Collections.ObjectModel; 
using System.Linq; 
using BusinessLib; 
using System.Windows.Input; 
using System.Windows; 
using System; 
using System.Collections.Generic; 

namespace TreeViewWithViewModelDemo.LoadOnDemand 
{ 
    public class PublicationsViewModel 
    { 
     readonly ReadOnlyCollection<CourseViewModel> _courses; 
     readonly ICommand _searchCommand; 

     public PublicationsViewModel(Course[] courses) 
     { 
      _courses = new ReadOnlyCollection<CourseViewModel>(
       (from course in courses 
       select new CourseViewModel(course)) 
       .ToList()); 
      _searchCommand = new TestIng(this); 
     } 

     public ReadOnlyCollection<CourseViewModel> Courses 
     { 
      get { return _courses; } 
     } 

     public ICommand SearchCommand 
     { 
      get { return _searchCommand; } 
     } 
    } 

    class TestIng : ICommand 
    { 
     public TestIng(PublicationsViewModel param1) 
     { 
     } 

     public bool CanExecute(object parameter) 
     { 
      return true; 
     } 

     event EventHandler ICommand.CanExecuteChanged 
     { 
      add { } 
      remove { } 
     } 

     public void Execute(object parameter) 
     { 
      MessageBox.Show("Into This Here"); 
     } 

     public void ShowThis() 
     { 
      MessageBox.Show("Into This Here Number2"); 
     } 
    } 
} 

나는 아마 난 단지 당신에게 코드의 일부를주는거야 사실 주어진 나에게 정확한 답을주고의 문제가 될 수 없습니다 것을 알고 있지만, 당신이주는 상관 없어 궁금 진행 방법 및 다음에 시도 할 수있는 것에 대한 아이디어를 알려주십시오.

XAML에서 CommandParameter를 정의하는 방법에 오류가 있다고 생각합니다. 어떻게 작동하는지 이해할 수 있을지 모르겠지만 "MouseDoubleClick"키를 누를 때 실행하려는 클래스의 메서드라고 생각합니다. 나는 뷰 모델의 두 가지 방법이 있습니다

 public void Execute(object parameter) 
     public void ShowThis() 

나는 간단한 테스트를 위해 사용하고 있습니다 ...하지만이 messagebox.show 라인은 실행되지 않습니다. 제가 아마도 계속하려면로 볼 수

 <Setter Property="local:CommandBehavior.CommandParameter" Value="ShowThis" /> 

을 또는 : 내가 말했듯이, 나는 오류를 받고 있어요 ...

사람은 내가 정의하는 방법에 몇 가지 통찰력으로 날을 제공 할 수 있습니다. 나는

+0

'전략'이 'null'이어야한다고 언급 한 해당 행에 예외가 발생하면 null 인 매개 변수는 execute 메서드 내에서만 결과를 가져옵니다. –

답변

0
당신의 샘플 코드는 클래스가 아닌 속성으로 TestIng 보여줍니다 SearchCommand 대신

TestIng의를 가리 키도록 결합 명령을 변경

...

감사 붙어있어, 당신은 바인딩 할 필요가 속성

관련 문제