0

I have a main view in which I have a tab control. The content of each tab is a treeview which is present in different views. This is my main view in which I use 2 other views

In my FirstListView, I have a tree view, a textbox and a button.

<TabControl x:Name ="MainTab" SelectionChanged="OnTabSelectionChanged">
    <TabItem Header="First" >
        <view:FirstListView x:Name="FirstView"/>
    </TabItem>
    <TabItem  Header="Second" >
        <view:SecondListView x:Name ="SecondView"/>
    </TabItem>
</TabControl>

Textbox and the button are added to perform a search in the tree.

The view model associated with the FirstListView has a command that is initialized in its contructor.

_searchCommand = new SearchFamilyTreeCommand(this);

SearchFamiltyTreeCommand is a class that is derived from ICommand and the execute method calls a function to perform the search. This is present in the FirstViewModel.

#region SearchCommand

public ICommand SearchCommand
{
    get { return _searchCommand; }
}

private class SearchFamilyTreeCommand : ICommand
{
    readonly FunctionListViewModel _functionTree;

    public SearchFamilyTreeCommand(FunctionListViewModel functionTree)
    {
        _functionTree = functionTree;
    }

    public bool CanExecute(object parameter)
    {
        return true;
    }

    event EventHandler ICommand.CanExecuteChanged
    {
        add { }
        remove { }
    }

    public void Execute(object parameter)
    {
        _functionTree.PerformSearch();
    }
}
#endregion

The search method is not type independent. It depends on the type present in its particular model. And the data required to perform the search is present in this view model.

This is working. Now I have to extend this functionality to other views ( SecondListView, ThirdListView and so on) which have their own treeviews(the type of the content is different from the FirstTreeView). How can I do it? Where shall I place the code and the command?

daniele3004
  • 13,072
  • 12
  • 67
  • 75
Jia Arora
  • 1
  • 1
  • **"SearchFamiltyTreeCommand is a class that is derived from ICommand and the execute method calls a function to perform the search."** Is this coded separately? If yes, I hope you can make use of it individually in your views . – Dinesh Kumar P Dec 05 '14 at 05:35
  • This is present in the FirstViewModel. I think I better add that code as well. – Jia Arora Dec 05 '14 at 05:39

2 Answers2

1

Don't place business Logic into ViewModels. ViewModels should be only for presentation logic.

Create a FamilyTreeSearchService and abstract it's functionality to this service, then inject the service into your ViewModels (i.e. Constructor, Dependency Injection, or ServiceLocator). Call the service from your ViewModels.

Tseng
  • 61,549
  • 15
  • 193
  • 205
0

1) Proper way:

Inherit your ViewModel classes directly from a common abstract base class. Refer this Stackoverflow Answer

2) Simple Way:

Have a separate class naming like 'CommonViewModel' and have common code in it. Inherit your additional ViewModel classes from the CommonViewModel;

Like below,

public class CommonViewModel
{
....
}

public class FirstViewModel:CommonViewModel
{
....
}
Community
  • 1
  • 1
Dinesh Kumar P
  • 1,128
  • 2
  • 18
  • 32
  • Ok. So you mean to say add a command in the base class? But I can't bind a command to a base class since the data context will be a derived class. Binding can't be done to a parent of a datacontext right? – Jia Arora Dec 05 '14 at 06:20
  • 1) All your ViewModel classes should inherit that abstract class, so that they contain Commands separately. 2) Implementation logic for those commands can be placed in a separate class(even service) and those methods can be accessed in each and every ViewModel. – Dinesh Kumar P Dec 05 '14 at 08:50