ToolBar as usercontrol

essamce 621 Reputation points
2020-05-09T01:02:00.97+00:00

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

<UserControl
    x:Class="SoilProfile.View.ReportEditToolbarView"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:local="clr-namespace:SoilProfile.View"
    mc:Ignorable="d">

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

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

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;
                #endregion
            }
        / ...

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);
            this.toolBars.ToolBars.Add(reportView.EditToobar);

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
    2020-05-10T10:46:35.91+00:00

    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