坚持从视图调用视图模型

时间:2014-04-14 03:46:44

标签: wpf prism

我有一个像下面添加了mousedouble的树视图

<TreeView 
        Grid.Row="0" 
        Name="tvTopics"            
        VerticalAlignment="Stretch" 
        HorizontalAlignment="Stretch" 
        MouseDoubleClick="tvTopics_MouseDoubleClick"
        ItemsSource="{Binding TierOneItems}"
        SelectedItemChanged="treeView1_SelectedItemChanged">
        <TreeView.ItemContainerStyle>
            <Style TargetType="{x:Type TreeViewItem}">
                <Setter Property="IsExpanded" Value="{Binding IsExpanded, Mode=TwoWay}" />
                <Setter Property="IsSelected" Value="{Binding Topic.IsSelected, Mode=TwoWay}" />
                <Setter Property="FontWeight" Value="Normal" />
                <Style.Triggers>
                    <Trigger Property="IsSelected" Value="True">
                        <Setter Property="FontWeight" Value="Bold" />
                    </Trigger>
                    <Trigger Property="IsExpanded" Value="True">
                        <Setter Property="IsSelected" Value="True" />        
                    </Trigger>
                </Style.Triggers>
            </Style>
        </TreeView.ItemContainerStyle>
        <TreeView.ItemTemplate>
            <HierarchicalDataTemplate ItemsSource="{Binding Children}">
                <TextBlock Text="{Binding Name}" />
            </HierarchicalDataTemplate>
        </TreeView.ItemTemplate>
    </TreeView>

在我的代码背后

 private void tvTopics_MouseDoubleClick(object sender, MouseButtonEventArgs e)
    {

        TreeView tv = sender as TreeView;

        if (tv.SelectedItem is TopicTreeItemViewModel)
        {
           Model.SelectedTopic = ((TopicTreeItemViewModel)tv.SelectedItem).Topic;

        }
     }

这里我试图将我的“主题”值传递给我的视图模型,但我不知道如何传递或调用我的视图模型方法。

    public class TopicTreeViewModel : NotificationObject, ITopicTreeViewModel
    {

        [ImportingConstructor]
        public TopicTreeViewModel(IGatewayService storyService, IEventAggregator eventAggregator)
        {
            this.storyService = storyService;
            this.eventAggregator = eventAggregator;

            this.AddTopicCommand = new DelegateCommand<object>(this.AddTopic);

            Helper.SubscriptionTokenList_LocationSearch.Add(this.eventAggregator.GetEvent<LocationSearchEvent>().Subscribe(OnLocationSearch, ThreadOption.UIThread));
            Helper.SubscriptionTokenList_SubjectSearch.Add(this.eventAggregator.GetEvent<SubjectSearchEvent>().Subscribe(OnSubjectSearch, ThreadOption.UIThread));

        }
        public void MouseDoubleClick(Topic topic)
        {
            if (topic != null && topic is Topic)
            {
                switch (this.searchType)
                {
                    case SearchType.Location:
                        this.eventAggregator.GetEvent<AddLocationEvent>().Publish((Topic)topic);
                        break;
                    case SearchType.Subject:
                        this.eventAggregator.GetEvent<AddSubjectEvent>().Publish((Topic)topic);
                        break;
                }
            }
        }

接口连接视图和视图模型

 public interface ITopicTreeViewModel
{
    ReadOnlyCollection<TopicTreeItemViewModel> TierOneItems { get; }

    ICommand SearchCommand { get; }

    string SearchText { get; set; }

    Topic SelectedTopic { get; set; }
}

所有我试图在这里做的是在触发moused双击事件时将主题值传递给我的视图模型。

我不知道如何传递或绑定此值。任何帮助非常感谢。

1 个答案:

答案 0 :(得分:1)

特别是在使用 Prism MVVM 时,建议尽可能添加实现背后的最小代码。因此,执行的每个逻辑或操作都将直接处理到 ViewModel

您应该将 MouseDoubleClick 事件绑定到 代理命令 ,而不是在View的代码隐藏上处理事件strong>在 ViewModel 中。因此,为了实现这一点,您需要将正确的 ViewModel 设置为视图 DataContext 。这样,绑定将通过 DataContext 实现解决。

以下 MSDN棱镜指南一章有助于了解查看 ViewModel 之间的互动:

此外,您还可以查看 MVVM Prism QuickStart ,并了解绑定 View-ViewModel 交互的实现方式

我希望这有帮助,问候。