How do I inherit views in WPF when using the MVVM pattern?

I am somewhat new to WPF and MVVM pattern. Here is my situation. My application is similar to Outlook (2007). There is a left navigation pane with a list of editors and a content pane on the right where the editors will appear in tabs. Some of my editors have a very similar look. They will have a top panel with a grid of elements for editing. Once you select an item, a form-style editor with text boxes, combo boxes, etc. appears in the bottom pane. I would like to encapsulate the basic functionality of these editors in a base class. But, from what I've read, you cannot inherit XAML. One solution I mentioned is to create a base UserControl class that has no XAML (only a .cs file). Then each of my editor views can inherit from it and include editor specific elements,especially in the bottom editing panel. Each of the editors will have their own view model, but they also inherit the common view model base class. Is this a good solution or is there a better way.

EDIT:

So my next question is: how? I created the following base class (no XAML) that dynamically creates 3 rows. The first line contains DataGrid

. The base class will define the grid, but the subclasses will set the binding. The second line just contains the grid separator. And the third line is empty. This is where the particular editor will place its own user control.

public class BaseEditor : UserControl
{
  public BaseEditor()
  {
    RowDefinition gridRow = new RowDefinition();
    RowDefinition splitterRow = new RowDefinition();
    RowDefinition editorRow = new RowDefinition();

    _userControlsGrid.RowDefinitions.Add(gridRow);
    _userControlsGrid.RowDefinitions.Add(splitterRow);
    _userControlsGrid.RowDefinitions.Add(editorRow);

    EditorGrid = new DataGrid();
    Grid.SetRow(EditorGrid, 0);
    _userControlsGrid.Children.Add(EditorGrid);

    GridSplitter gridSplitter = new GridSplitter();
    Grid.SetRow(gridSplitter, 1);
    _userControlsGrid.Children.Add(gridSplitter);

    AddChild(_userControlsGrid);
  }
}

      

Then I tried to create a custom editor that inherits from BaseEditor. I have set base class in cs file in BaseEditor and I have set begin / end tags in xaml in BaseEditor as well.

I can see the controls in the designer of a particular editor. However, I soon see that this is not what I expected. If I start adding content in a specific XAML editor, it won't be on the third line. So, I am missing something. In window forms, you can create a content area using attributes and a developer can edit that area. How do I do this in WPF so that all content ends up in the third line?

+3


source to share


2 answers


I would have thought it was more like a set of wrapped controls, each containing a section of content.

If you think of it more like "Button" then you can get my point. The WPF button has a specific style and layout that is called when you paste <Button>Content</Button>

into your XAML. Each button also has a Content section that defines where the XAML in it displays your specified content. Your overall layout can be structured to "just control" like a button, but with built-in functionality.



Then you can add your specific content to each one. Your control doesn't have to be its own, but it can certainly contain views with appropriate ViewModels as needed.

So, your toolbox is a virtual machine view that has several Tool controls for general layout. Each tool has a Content section with each tool-specific View control.

0


source


Inheriting views from a custom base class is a great solution for general presentation logic. Create custom UserControls or create advanced templates to create view objects that you want to reuse across many views. Then bind UserControls to the view data.



0


source







All Articles