【问题标题】:Treeview events with Attached Command Behavior带有附加命令行为的树视图事件
【发布时间】:2012-03-22 11:21:42
【问题描述】:

我想用 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

谁能帮我装订?

问候,

米歇尔

【问题讨论】:

  • 还有你的 SomeCommand 属性被定义了吗?在 MyDataType 上?

标签: wpf treeview attachedbehaviors


【解决方案1】:

由于此绑定已损坏,因此抛出了不太具有描述性的异常:acb:CommandBehavior.Command="{Binding SomeCommand}"

所以 WPF 找不到您的 SomeCommand 属性。我想问题出在HierarchicalDataTemplate 周围,所以DataContext 不是您所期望的...

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

【讨论】:

  • 现在,这很方便!我现在没有输出窗口显示绑定错误。 System.Windows.Data 错误:40:BindingExpression 路径错误:在“对象”“MyDataType”(HashCode=47761)上找不到“SomeCommand”属性。 BindingExpression:Path=SomeCommand; DataItem='MyDataType' (HashCode=47761);目标元素是'StackPanel'(名称='');目标属性是“Command”(输入“ICommand”)
  • 现在的问题是:您的SomeCommand 定义在哪里,因为错误不在MyDataType 上,而是当前的DataContext。如果你修复它,那么acb:CommandBehavior 应该可以工作。
  • 我遗漏了一些东西,我想在视图模型而不是我的业务实体上使用命令。如何定位虚拟机?
  • 没有大局观:viewmodel 和 businessentities 是如何实现的,你在哪里设置 DataContexts 等等。很难说...请更新原始文件或创建一个新问题.. .
  • DataContext 是 ViewModel,但是树视图 DataContext 是成员 ViewModel.MyDataType。所以现在的问题是如何在 datacontext 设置为成员的情况下访问 VM。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-12-11
  • 1970-01-01
  • 2013-06-28
  • 2023-03-13
  • 2014-03-21
  • 2019-12-18
相关资源
最近更新 更多