【问题标题】:Caliburn Micro: How to bind ActivateItem to selectionEvents in ConductorsCaliburn Micro:如何将 ActivateItem 绑定到导体中的 selectionEvents
【发布时间】:2012-07-03 12:10:34
【问题描述】:

从 SimpleMDI Caliburn Micro 项目中可以看出,有一些约定将 tabControls 中的 selectionEvent 绑定到 Conductor 中的 ActivateItem。我真的看不出有人提到这个事件可能是什么。

但是,当您的控件不能满足这些约定时,我会不知道如何自己管理它们。

我有一个 Telerik RadTreeView,我想用一个导体来管理它,以便能够按需加载节点的子节点(通过 WCF 调用)。

这是我所在的地方:

<telerik:RadTreeView x:Name="Items"
                     cal:Message.Attach="[Event Selected] = [ActivateItem($dataContext)]"  />

传递 $dataContext 是错误的,因为这样他传递了 Conductor 本身,$SelectedItem 返回 null。

所以我比较简单的问题是三方面的。

1) 如果 RadTreeView 是一个选择器,为什么基本的 CM 约定不能使用它,

2) 我应该使用什么事件来调用 ActiveItem

3) 我可以传入什么。

【问题讨论】:

  • 您是否在显示分层数据?
  • 我实际上已经改变了它的工作方式。是的,我想显示分层,但它都会实现 IActivate 的东西。问题是将树视图中的事件与导体的激活逻辑连接起来。我不知道如何将所选项目作为参数传递。

标签: wpf caliburn.micro rad-controls convention-over-configur


【解决方案1】:

This 可能有助于理解执行树视图和 mvvm 的不同方法。

我也在使用 RadTreeView,我最终将事件发送到托管 Items 集合的 ViewModel TreeViewItemViewModel。当一个动作/事件(例如 Edit)被发送到 MainViewModel 时,我有一个类似的方法:

 public void Edited(object sender, RadTreeViewItemEditedEventArgs e)
        {
            var treeViewItemViewModel = e.NewValue as IEditable;
            if (treeViewItemViewModel == null) return;

            treeViewItemViewModel.EndEdit();
        }

所以这适用于树中的任何级别,也适用于具有不同的行为,检查不同事物的接口是否已实现。

RadTreeView 的 xaml

 <telerik:RadTreeView x:Name="MyTree"
                                     Grid.Row="1"
                                     Margin="0,20,0,0"
                                     VerticalAlignment="Stretch"
                                     FontSize="16"
                                     FontFamily="{StaticResource MainFontFamily}"
                                     ItemsSource="{Binding Children, Mode=TwoWay}"
                                     ItemTemplate="{StaticResource HierarchicalDataTemplate}"
                                     ItemEditTemplateSelector="{StaticResource ItemEditTemplateSelector}"
                                     ItemEditTemplate="{x:Null}"
                                     IsLoadOnDemandEnabled="True"
                                     IsEditable="True"
                                     IsDragDropEnabled="True"
                                     DropExpandDelay="00:00:01"
                                     telerik:TextSearch.TextPath="ItemId"
                                     PathSeparator="|"
                                     cal:Message.Attach="
                                    [Event LoadOnDemand] = [Action LoadOnDemand($eventArgs)];
                                    [Event PreviewDragStarted] = [Action PreviewDragStarted($source,$eventArgs)];
                                    [Event PreviewDragEnded] = [Action PreviewDragEnded($source,$eventArgs)];
                                    [Event DragEnded] = [Action DragEnded($source,$eventArgs)];
                                    [Event Edited] = [Action Edited($source,$eventArgs)];
                                    [Event EditCanceled] = [Action EditCanceled($source,$eventArgs)]"/>

【讨论】:

  • 那么 Caliburn.Micro 应该可以帮助您避免这样的混乱。因此,您可以创建一个普通方法并将某些内容作为参数传递。参数应该是我刚刚选择的项目,但我希望我可以在 cal:Message.Attach
  • 我正在使用 Message.Attach。 ` [Event Edited] = [Action Edited($source,$eventArgs)];` 我进行了编辑以显示 TreeView 的 xaml。
  • 如果您还有其他问题,请告诉我,我可以提供更好的示例。
猜你喜欢
  • 1970-01-01
  • 2016-08-10
  • 1970-01-01
  • 1970-01-01
  • 2020-10-24
  • 1970-01-01
  • 1970-01-01
  • 2015-01-20
  • 2011-06-29
相关资源
最近更新 更多