Usher
Usher

Reputation: 2146

stuck to call view model from view

I have a tree view like below added mousedouble click

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

on my code behind

 private void tvTopics_MouseDoubleClick(object sender, MouseButtonEventArgs e)
    {

        TreeView tv = sender as TreeView;

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

        }
     }

here i am trying to pass my "topic" value to my view model but i have no idea how to pass or call my view model method.

    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;
                }
            }
        }

And the interface connect between view and view model

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

    ICommand SearchCommand { get; }

    string SearchText { get; set; }

    Topic SelectedTopic { get; set; }
}

All im trying to do here is passing the topic value to my view model when the mousedouble click event triggered.

I have no idea how to pass or bind this value. any help much appreciated.

Upvotes: 0

Views: 316

Answers (1)

GOstrowsky
GOstrowsky

Reputation: 989

When using Prism and MVVM in particular, it is reccomended to add the minimal code behind implementation as possible. Therefore, every logic or action performed would be handled directly into the ViewModel.

Instead of handling the event on the View's Code Behind, you should bind the MouseDoubleClick event to a Delegate Command in the ViewModel. So, in order to achieve this, you would need to set the proper ViewModel as the DataContext of the View. This way, Binding would be resolved through the DataContext implementation.

The following MSDN Prism Guide chapter would be helpful to understand the interaction between View and ViewModel:

In addition, you could take a look at the MVVM Prism QuickStart and undestand how the Binding to the View-ViewModel interaction is implemented.

I hope this helped, Regards.

Upvotes: 1

Related Questions