sharp
sharp

Reputation: 1211

WPF ToolBarControl in multiple UserControls

I'm working on a project where the request is to implement ToolBarControl in multiple UserControls. The UserControl would mostly have that toolbar and GridView (Devexpress). I'm using WPF with MVVM and Caliburn.Micro framework for development.

The problem is that, that I need to c/p code of the ToolBarControl in the XAML and then in ViewModel to implement the properties.

I'm searching for a better way, and for now I guess it would be reflection. Any advice would be helpful, code examples too.

Update #2

The controls who would be in the custom toolbar, would should be able to move selected row up or down, delete item, edit and create (last two should open a new window). Let's say I have CustomersListViewModel, who in it's CustomersListView has the custom ToolBarControl and GridControl. When I click add button, it should open me CustomersEditViewModel. When I click delete, it should delete selected item in list. When I click move up, it should move up selected row.

Upvotes: 1

Views: 331

Answers (1)

user5747953
user5747953

Reputation:

You could use a datatemplate toolbarviemodel and toolbarview in your app.xaml and then use a contentcontrol to show the toolbar binding it to an instance of your toolbarviewmodel

app.xaml:

<ResourceDictionary>
 <DataTemplate DataType="{x:Type ViewModelToolBar}">
        <startViews:ViewToolBar />
    </DataTemplate>
 </ResourceDictionary>

and in your usercontrol:

<ContentControl Content="{Binding MyViewModelToolBar}"/>

and to execute your commands you could use a notify event with a tag or so as parameter to tell your usercontrol viewmodel wich operation should be performed.Means you bind your toolbar buttons to the notifycommand and use button name or tag as parameter.

ViewModelToolBar:

 public event EventHandler Notify;

    private void OnNotify(object sender)
    {
        Notify?.Invoke(sender, new EventArgs());
    }

    public ICommand NotifyCommand => new DelegateCommand<object>(OnNotify);

and in your usercontrol ViewModel:

 MyViewModelToolBar = new ViewModelToolBar();
 ViewModelToolBar.Notify += ViewModelToolBar_Notify;

  private void ViewModelToolBar_Notify(object sender, EventArgs e)
        {
            switch (sender.ToString())
            {
                case "Case1":
                   "perform your operation"
                    break;
                case "Case2":
                   ...
                    break;
                case "Case3":
                   ...
                    break;
                              }
        }

Upvotes: 1

Related Questions