Question

I want to handle events on a treeview with ACB (http://marlongrech.wordpress.com/2008/12/04/attachedcommandbehavior-aka-acb/).

I am stuck with the bindings in the XAML file. The event is fired but I keep getting null reference exceptions in the ACB library because strategy is null:

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

In the XAML file I added the following (excerpt):

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">

And in the Viewmodel I added:

        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

Who can help me out with the binding?

Regards,

Michel

Was it helpful?

Solution

The not too descriptive exception is throw because this binding is broken: acb:CommandBehavior.Command="{Binding SomeCommand}".

So WPF could not find your SomeCommand property. I guess the problem is around the HierarchicalDataTemplate so the DataContextis not what you would expect...

Check for binding errors in the Visual Studio's Output window during runtime and you will know what to fix then it should work.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top