I need to develop an multipage WPF application. I tried to achieve that by using one Window and multiple Pages. When I need to switch from one to another page I just change Content of main Window to point to that page. I have filling that I'm using wrong scenario because I don't have idea how to reuse XAML code for example an Menu controll, StatusBar and ToolBar for all pages.
You need ContentControl. It changes view based on ViewModel. Place your static elements in one place and in changeable area a ContenControl object.
More here and here.
One idea is you have a PageViewModelBase that implement a base virtual Property and Methods for Menu and StatusBar model and ... Then Each PageViewModel Override proper implementation of PageViewModelBase, Then Window contains Menu or StatusBar that Bind to DataContext of Pages.
Related
I'm on writing a simple application, it has a menu and when user choices each MenuItem, i want to change my window's content to display the selected content.
i have two option to that.
i can add a <Frame></Frame> to my window and write some pages.
i can write some UserControls and put them in a ContentControl
as user fires MenuItem click event.
so I'm confused to select the right choice for this purpose.
Navigation can be succefully implemented by using Frame/Pages or ContentControl/Views. It is a matter of choice.
However, Frame/Page have some gotchas, e.g. page.DataContext not inherited from parent Frame?
If you don't need isolation specifically, then stick to ContentControl. Navigation in prism framework is built with regions which are located in different type of controls (e.g. ContentControl, TabControl), not Frame (see docs)
one more approach for simple navigation is ViewModel based.
Examples:
WPF MVVM navigate views
Navigation with MVVM by Rachel Lim (external)
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.
I have a MainWindow in my WPF app.
This MainWindow has a menu on the left and when a menu option is selected a UserControl is loaded on the right.
This is similar to Master Pages in asp.net
What I want to do now is to have a modal window show from the UserControl which will only allow the user to interact with the modal window.
I have seen examples of the Main Window showing a modal window (http://www.codeproject.com/Articles/36516/WPF-Modal-Dialog) but not sure on how to load this from a UserControl.
There's this: dialogs and mvvm but this is the best example I've seen of dealing with it: mvvm and closing forms
The first link I've not used and stumbled across while looking for the second link to post that here. The second link has two downloads, you can ignore the _service download, it's basically the same.
One way in WPF is this method
Add a new Window to the project.
Add other controls onto the window as needed.
In XAML name the window such as x:Name="MyWindow"
Put on Dependency properties on the window and have each of the controls bind to the window's data context such as {Binding MyText, ElementName=MyWindow}. (Note I still use, even for WPF these Visual Studio code snippets to add different dependency properties, these save time for a very redundant operations of adding them: Silverlight Snippets.
In the location where you want to launch the model dialog use this example:
Example:
var about = new About(); // Create the new window
// I've added a CompanyName dependency property.
about.CompanyName = "OmegaMan Industries";
about.ShowDialog();
I have a custom UserControl that I created as a navigation menu that parses an xml file and populates itself with hyperlink buttons. So basically my control is an empty stackpanel, and when it's loaded it adds hyperlinkbuttons as children to the stack panel.
In my application I just add a <myLibrary:NavigationMenu links="somexml.xml" />
The problem is that I want to be able to style the hyperlinkbuttons and the stack panel differently for every application. What is the best way to do this.
In the code behind for the control, create a DependencyProperty of type Style for both HyperlinkStyle and StackPanelStyle. Then when you create the items apply the correct styles too them.
Take a look at MSDN
The article is a good starting point for writing stylable controls.
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.