具有附加命令行为的Treeview事件

时间:2012-03-22 11:21:43

标签: wpf treeview attachedbehaviors

我想用ACB(http://marlongrech.wordpress.com/2008/12/04/attachedcommandbehavior-aka-acb/)处理树视图上的事件。

我坚持使用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">

在Viewmodel中我添加了:

        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

谁可以帮我解决问题?

此致

米歇尔

1 个答案:

答案 0 :(得分:1)

不太具描述性的异常是throw,因为这个绑定被破坏了:acb:CommandBehavior.Command="{Binding SomeCommand}"

因此WPF无法找到您的SomeCommand媒体资源。我想问题出在HierarchicalDataTemplate左右,所以DataContext不是您所期望的......

在运行时检查Visual Studio的“输出”窗口中的绑定错误,您将知道要修复的内容然后它应该可以正常工作。