Re-using Buttons in WPF - wpf

I have a bunch of different objects that are commonly edited in the same TabControl using different DataTemplates, but I want each DataTemplate to have a common look and feel with Ok and Cancel buttons at the bottom right of each tab that will close the tab or save the content and then close the currently selected tab. What's the best way to place buttons on each tab ? Is there a way to do it without copying and pasting the buttons and stack panel across all of my data templates ?

Sure, you can create your own OkCancelSaveControl. In WPF, creating a "user control" is much easier than it sounds. Here is a tutorial. In a nutshell, you
create a new user control,
create properties in the user control that give the your control the information it needs to perform its duties (e.g. the tab that it's supposed to close or the data object that it's supposed to save),
if necessary, create events that the user control raises (OkClick), in case some tab requires special treatment.

I would make a custom control, lets call it MyCoolTabItem, that inherits from the TabItem class, and just throw your buttons into the control. Then just add a MyCoolTabItem instead of a TabItem to all of your TabControls and it will have all of your buttons on it.

You could make a base view class that held those buttons. Views that needed the buttons would inherit them and common functionality.

Related

What's the UI design pattern for displaying content in a window when menu item is selected?

I am new to Windows Forms. I have a menu in a form. When a user selects a menu option I want to display some elements like a grid, treeview or a grouped UI elements.
What is the UI design pattern for displaying the view for the selected option? Load a user control dynamically in a pane? Show a form and hide a previous form? I am not using tab control. Content is displayed in a pane in the form. It's not a separate window.
I have searched high and low for sample applications and I couldn't find any which has a menu. Any ideas?
Constructing your form as needed in designer and then hiding and showing sounds like a reasonable approach.
I definitely would avoid dynamic content loading. In WinForms you are most likely relying on events to handle UI interaction. If loading/unloading controls dynamically you would have to take care of hooking/unhooking event handlers. It's easy to keep track of that when you have 3 controls each with single event. But if you have more controls and each controls has to take care of many events the loading/unloading and hooking/unhooking events is going to be error prone. Also unhooked event handlers will result in memory leaks. Other problem is that your complex controls will have many properties. All of them will have to be set up in code. You will end up with dozens of lines listing controls' properties and assigning values to them.
Hiding/showing doesn't expose you to these issues. You design your layout once in the designer. So your main code is not clattered with pure UI construction. Also, you do not create a new instance of a control when you show it so you can subscribe event handlers to events at design time and you do not have to worry about unhooking the handlers when hiding. You create one instance of a control and rely on this instance throughout application lifetime.

Hide/Show user control, or dispose user control better approach

I have a WPF Winform application. The form has a header on the top and a user control below. Once i am done entering values in the user control, i want to dismiss this user control and add another user control in that area. I will have 4 user controls in a sequence and I may have to move back to the previous user control or move forward.
Which is the better approach to achieve this in MVVM? To hide and view the user controls or dispose the user control.
Hide. Its simpler.
Also your description sounds remarkably like a wizard, so you might want to look at using a NavigationFrame as this would then allow you to "navigate" to the next set of controls once once set is complete.
P.S. Navigation frames can very easily be styled to completely remove the navigation bar UI, see http://winchrome.codeplex.com/ for some examples

How to bind an element from a user control to a different user control?

I need to create a basic user control with certain functionality (like dragging, minimizing and more..).
Now I need a bunch of other user controls to inherit from it and add their content.
I want an option to add new user controls that implements only a part of the base control and remains with the rest of the elements and functionality.
How can I bind a certain element (grid, stackpanael,.. whatever..) from the base user control to the new control? (without loading xaml in the code behind)
I basically want to "plant" a xaml element in a dedicated element on the base user control from a different user control.
If you write a custom control, one of the Controls you can use in the template is called a "ContentControl." Then when you use the Custom Control on a page, you can specify in xaml what the content is going to be. Thus you can put in a Grid, StackPanel, whatever in there.
But note that writing your own Custom Control is rarely done other than by 3rd party component vendors, as it requires a fairly deep knowledge of SL (for example, how to use Dependancy Properties). More commonly used is the "User Control" which is far easier to use and requires no such knowledge. I usually start by trying to accomplish what I need with a UserControl, and if I hit a roadblock, then switch over to a CustomControl.
But most of the existing controls already come with the functionality you describe - drag and drop, minimize etc. SL has a rich UI control set already, so a good place to start is by seeing if one of the existing controls has most of what you need, and then take it from there.
Greg

Creating a "mergeable" toolbar with Prism regions?

I'm developing a WPF application where I would like a common toolbar along the top of the screen (when I say "toolbar" it won't be a WPF ToolBar control, more likely just a series of image buttons resembling a Windows 8 app bar). I'm using Prism navigation.
What I had in mind was that this toolbar would live in the main window, and always be visible throughout the application. The toolbar would include a couple of standard buttons such as "Exit" and "Help".
Below the toolbar, the main window essentially just contains a large Prism region. When I navigate this region to a view (call it "view1") I want view1 to add additional buttons into the toolbar.
Now, "view1" may have Prism regions of its own, and when one of these is navigated to a view (call it "view2"), view2 should be able to add buttons of its own, alongside the "standard" main window buttons and the buttons added by view1.
It goes without saying that the relevant buttons should be removed when navigating away from a view.
I'm sure I could roll my own solution, but wondered if I could simplify things with Prism? I thought about putting a Prism region in the toolbar alongside the "standard" buttons. "view1" would then navigate this region to a view that basically just contains view1's buttons. This "view1 button view" could itself contain a region, that view2 could navigate to its own "button view". Is this viable, or is it going to get too complicated?
It sounds like you might be complicating it a bit, or at least you lost me at the end...but that doesn't take much today!
A suggestion: Your "toolbar" could be, for example, some ItemsControl where your ItemsSource is a collection of some class ToolBarOperation. This class could contain a description to display to the user and an ICommand to perform when clicked. Style your ItemsControl's items to be buttons and bind each button's command to your class's ICommand. This collection would be populated by the currently visible view's viewmodel (i.e. View1's viewmodel would already know what commands it would be responsible to perform. When View1 is loaded, fill the collection with ViewModel1's list of ToolBarOperation.)
So, to answer your question, I don't think you need anything Prism specific (except maybe their implementation of DelegateCommand)...the "Controller pattern" discussed in the documentation might be helpful. But it shouldn't be hard to accomplish what you want with Prism alongside.

how can i change controls when a button is pressed in xaml

i am trying to create a wpf app and have different parts in user controls.
in the navigation i have some buttons (now using the ribbon ctp). is it possible to change the main user control when different buttons are pressed in xaml. or is this just a bad way to do things?
sorry, really new to xaml and im trying to get my head arround it.
Further to what Carlo has said,
The way we do it is to have a blank grid in the place you want your controls to all appear and then use BlankGrid.Children.Clear() and BlankGrid.Children.Add() to set up which control is visible in this position.
We found that was the nicest programatically as we have a large number of custom controls, but Carlo's method would work nicely if you wanted to use the designer.
I think this is a pretty regular procedure in WPF. In my experience, me and other programmers put the controls where we want to show them and make their visibility hidden, collapsed or visible depending on what we want to show the user.

Resources