Question

I'm using WPF and the TabControl as my Menu. This generates a lot of code behind code because one file is controlling all the user actions. Then I was thinking that I could use a Controller class for every TabItem. I used it and it works, but I don't like the outcome. I have to use the FindName() method on the TabItem that I pass to the Controller and it generates some ugly code that can be hard to debug (in my opinion at least).

So is there any way to pass the TabItem so that I can find the Controls on the xaml page in my Controller?

For example I have TabItem1, TabItem2 and TabItem3. Each containing a page for the user to use. Then I want to pass TabItem1 to controller TabItem1Controller. At the moment I'm doing it this way:

public TurbineController(TabItem tab)
    {
        _client = tab;
    }

But then I have to use the FindName() method.

Is my request possible?

Was it helpful?

Solution

You got it backward, at least to the MVVM approach. The controller (TurbineContoller) shouldn't reference the view (TabItem), the view should reference the controller. Create a collection of controllers and databind the collection to a tabcontrol. The datacontext of each tab will be the controller which the tab will databind to.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top