ToolBar as usercontrol

essamce 621 Reputation points

i'm building kinda large scale wpf (with mvvm) project and i want to define some ToolBars for some different/logically separated views, then collected them in the application mainwindow(main view), here is one of them:

    xmlns="" xmlns:x=""
    xmlns:mc="" xmlns:d="" xmlns:local="clr-namespace:SoilProfile.View"

    <ToolBar x:Name="EditToolbar"
        Band="1" BandIndex="1">
            ToolTip="Add New Page" Content="Add Page" />
            ToolTip="Delete Page" Content="Delete Page" />
        <Separator />
            ToolTip="Add New Layer" Content="Add Layer" />
            ToolTip="Delete Layer" Content="Delete Layer" />

        <!--<Button ToolBar.OverflowMode="Always">

then in some view i want to use this toolbar, like:

 /// <summary>
        /// Interaction logic for ReportsView.xaml
        /// </summary>
        public partial class ReportView : UserControl, ViewInterface.IReport
            private ReportVM _viewModel;
            public ToolBar EditToobar { get; internal set; }

            public ReportView(ReportVM viewModel)
                // ...

                #region initializes
                var toolbarView = new ReportEditToolbarView();
                EditToobar = toolbarView.EditToolbar;
                toolbarView.Content = null;
        / ...

then in the main window i have a

<ToolBarTray x:Name="toolBars"

and in code behind:

var reportVM = new ReportVM();
            var reportView = new ReportView(reportVM);

my questions are:
Q1: is that a good practice to use toolbar as a usercontrol.content (if not , what is the better way),
Q2: is it necessary to have ViewModel class for each view even simpler one (like a toolbar).

any suggesions will be appreciated,
thanks in advance.

Windows Presentation Foundation
Windows Presentation Foundation
A part of the .NET Framework that provides a unified programming model for building line-of-business desktop applications on Windows.
2,710 questions
0 comments No comments
{count} votes

Accepted answer
  1. Peter Fleischer (former MVP) 19,316 Reputation points

    Hi, if you plan to use the toolbar in several windows or to create it by several developers, to use a user control is a very good way.

    You can use for each View one or more ViewModels or for more Views only one ViewModel. One ViewModel should serve one or more business processes, no matter how many views are required in this business processes.

    1 person found this answer helpful.

0 additional answers

Sort by: Most helpful