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)
Related
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.
I need a listview with multiple buttons for scrolling. E.g.
ScrollToTop Button
ScrollUp Button
ListView
ScrollDown Button
ScrollToBottom Button
I have got the buttons working in a WPF application by using the code mentioned here. Now, I need to reuse this by making it a control(lookless?). The layout of the listview and buttons can be horizontal or vertical. Should I use custom control or user control?
Here's what I'd recommend.
Don't use any pre-composed elements. Create a behavior ScrollList, accepting two parameters - Direction and Target, Direction will be Top || Bottom, Target you list - again you can use ElementName binding.
The reason why I'd recommend this approach is the actual code required to scroll your list is tiny, while managing layouts via properties in WPF is proved to be mess an anti pattern (yes you can go ControlTemplates, but it's definitely too havy for what you're trying to do).
If behaviors are too complex just consider creating a couple of commands.
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.
What is the difference between a ContextMenu class and the Popup class?
The MSDN docs do a nice job of displaying the distinction:
The Popup Class:
Represents a pop-up window that has
content.
The ContextMenu Class:
Represents a pop-up menu that enables
a control to expose functionality that
is specific to the context of the
control.
So the ContextMenu is a more-specific version of a Popup - it's meant to be bound to a specific control, providing ways to interact with that control. Read further on the MSDN page: the ContextMenu has built-in facilities for displaying itself when you right-click on the associated control, and it is automatically displayed within a Popup.
The Popup class is much more general: it simply defines a barebones window (no default borders or decoration) that can display any arbitrary UIElement on top of other controls (notice that the Popup class is part of the Primitives namespace, meaning it's meant to be part of the composition of other controls, such as the ContextMenu).
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.