2012-03-22 75 views
1

我想用ACB處理樹視圖上的事件(http://marlongrech.wordpress.com/2008/12/04/attachedcommandbehavior-aka-acb/)。帶有附加命令行爲的TreeView事件

我被卡在XAML文件中的綁定。該事件被觸發,但我一直在ACB庫得到空引用異常,因爲戰略是空:

/// <summary> 
    /// Executes the strategy 
    /// </summary> 
    public void Execute() 
    { 
     strategy.Execute(CommandParameter); 
    } 

在我添加以下內容(節選)XAML文件:

xmlns:acb="clr-namespace:AttachedCommandBehavior;assembly=AttachedCommandBehavior" 

    <StackPanel x:Name="VerklaringenTreeviewPanel"> 
    <Border x:Name="TreeviewHeaderBorder" Style="{StaticResource TreeviewBorderHeaderStyle}"> 
     <TextBlock x:Name="tbTreeviewHeader" Text="Verklaringen concept" Style="{StaticResource TreeviewHeaderStyle}"/> 
    </Border> 

    <TreeView x:Name="MyTreeview" ItemsSource="{Binding}" Style="{StaticResource TreeviewStyle}"> 
     <TreeView.Resources> 
      <ResourceDictionary Source="..\Themes\TreeviewItemStyle.xaml" /> 
     </TreeView.Resources> 
    </TreeView> 

    <StackPanel.Resources> 
     <HierarchicalDataTemplate DataType="{x:Type local:MyDataType}" ItemsSource="{Binding MyChildDataType}"> 
      <StackPanel Orientation="Horizontal" acb:CommandBehavior.Event="MouseDown" acb:CommandBehavior.Command="{Binding SomeCommand}" acb:CommandBehavior.CommandParameter="Hi There"> 

並在視圖模型我補充說:

 Public Property SomeCommand() As ICommand 
     Get 
      Return _someCommand 
     End Get 
     Private Set(value As ICommand) 
      _someCommand = value 
     End Set 
    End Property 

    Public Sub New() 
     MyBase.New() 

     Dim simpleCommand As SimpleCommand = New SimpleCommand() 
     simpleCommand.ExecuteDelegate = Sub(x As Object) 
              Dim test As String 
              test= "noot" 'I want to hit this breakpoint 
             End Sub 
     Me.SomeCommand = simpleCommand 
    End Sub 

誰能幫我一起裝訂?

問候,

米歇爾

+0

您的SomeCommand屬性是否定義?在MyDataType上? – Nikolay 2012-03-23 06:31:14

回答

1

不太描述性的例外是拋出,因爲這種結合被打破:acb:CommandBehavior.Command="{Binding SomeCommand}"

所以WPF找不到你的SomeCommand屬性。我猜這個問題是在HierarchicalDataTemplate左右,所以DataContext是不是你所期望的...

在運行時檢查Visual Studio的輸出窗口中的綁定錯誤,你會知道該怎麼修復,那麼它應該工作。

+0

現在,這很方便!我現在沒有輸出窗口顯示綁定錯誤。 * System.Windows.Data Error:40:BindingExpression path error:'SomeCommand'property not found on'object'''MyDataType'(HashCode = 47761)'。 BindingExpression:路徑= SomeCommand; DataItem ='MyDataType'(HashCode = 47761);目標元素是'StackPanel'(Name ='');目標屬性是'命令'(鍵入'ICommand')* – 2012-03-23 06:49:07

+0

現在的問題是:你的'SomeCommand'定義在哪裏,因爲錯誤不在'MyDataType',它是當前的'DataContext'。如果你修復它,那麼'acb:CommandBehavior'應該可以工作。 – nemesv 2012-03-23 06:59:02

+0

我錯過了一些東西,我想要在viewmodel上的命令,而不是我的businessentity。我如何瞄準虛擬機? – 2012-03-23 07:00:09