Should I be using a WPF user control? Or just a Grid? - c#

So I have an application that is split into 2 parts - on the left there is a custom menu, on the right a grid that holds all the "content" (different screens).
It looks something like this:
Also, when the different buttons are hit, the menu on the left will fill up with different buttons (for example, if you hit the review button, the menu would become something along the lines):
Start Date
End Date
Employee
Project
...
I am pretty sure that I want each of the screens (to go on the right) to be their own user controls.
But my question is this: Should each of the menu's be user controls? This makes it a little harder to use them. Then I have to worry about having getters/setters so the main window can listen on the menu's buttons, etc.
The other option is to just programmatically add the buttons in the mainWindow, this way I can just add the listeners right in mainWindow.cs (into a grid)
Which is the better method? Or is there another method which is favoured?

The entire window can be done easily as a Grid.
The left side can just hold a StackPanel or other layout control with your buttons.
You can use a ContentPresenter to hold the content on the right. When you trigger your buttons, just change the bound content, and it will update with your appropriate user controls.

I think you might do it with a grid as Reed told. But if you will need to reuse this in another place, I think you should use a separated UserControl instead.

Maybe I'm misunderstanding you, but I would use a TabControl, with TabStripPlacement="Left".
If you need extra space on the left to display other stuff, you can override the default TabControl's ControlTemplate and add a bunch of margin space to the TabPanel inside the ControlTemplate. Alternatively, you could have the TabItem's header display differently when it is selected versus when it is not selected.

Related

How make an area on win form scrollable?

Items like below have to be accommodated on a specific area on WinForm.
"Order 1 ------------ Time Left: 00:20:00"
"Order 2 ------------ Time Left: 01:30:20"
We should be able to perform the following action on the each order:
Each item will occupy not more than one line. As the area specified
for it on the win form is limited, as more items comes in, I want to
make the area scrollable.
According to the time left the background color of the line should also change.
There is a DONE button next to it. So, if the item is selected and DONE is pressed, the item is moved off the list.
My question is what C# Form control can be used for it. I was thinking of labels, but how to make the area scrollable if there are many of them. If not labels, what else is suggested?
As suggested in comments, you can use grid, but in case it does not suits your requirements, this is like something what you can do -
Create a custom user control which will have a label control for your order detail and a Done button. Something like this (sure you will design it better!). The label will be empty initially and you will pass then from outside, using a public property for example, while creating the control.
Define an Event on this control, and raise this event when the Done button is clicked. This is required for your main form to know that when to remove this user control.
Add a FlowLayoutPanel to your main form. This will be the container for your user controls. Make sure to set the following so that the controls are created as desired
this.flowLayoutPanel1.FlowDirection = FlowDirection.TopDown;
this.flowLayoutPanel1.WrapContents = false;
this.flowLayoutPanel1.AutoScroll = true;
Now you can start adding your custom control to this FlowLayoutPanel, either by loop or the way you like. The control will added in linear way, one in each line, and you will also get scroll it it exceeds the given space.
Make sure to define event handler for the control, so that you know when to remove the control. And of course you can set other properties like back groung color etc. That's not going to be any problem.

WPF 3 panels with seperate content or 1 panel with dynamically generated content

I am creating WPF application that utilizes a simple navigation menu. When one of the menu items is selected, I want a grid control with buttons to become visible.
I have 3 items on my navigation menu. For purposes of speed and better coding practice, is it better to have 3 seperate grids and they all are visible until the button is clicked and one becomes visible or is it better to have just one whose content gets changed depending on what button is clicked?
I'd prefer to use a container control (e.g. ListBox) instead of a grid. This way you can bind one of your three menus to this container control's DataContext, depending on the button clicked.
If your menu entries are static you could also use a content control and change the ContentTemplate. All three neccessary ContentTemplates can then be defined in XAML.

C#, WPF, MVVM, Creating a viewmodel for message dialogs/confirmation dialogs?

I am not using anything other than a simple WPF application project in visual studio. I've implemented an mvvm application.
I want to display a list of content changes made by a user. I have a main window view model and it currently just builds a strings with changes. I have objects that I can reuse to display their properties (the content).
Currently, I use a MessageBoxResult to show a really long string with the changes. This is a terrible design (I know), but I couldn't really find an answer to what class a regular wpf project has that would allow me to achieve what I want.
I know there is a popup class I can use. In practice, which is better-- another view model for the dialog, or a popup?
Can anyone provide a simple example of one of the two approaches?
Thank you in advance for your response.
What I've done in the past is have a simple Border control, and inside of a TextBlock and whatever Button controls I need. I bind the TextBlock.Text to a public string property named "MessageBoxMessage" which calls OnPropertyChanged(). I bind the Command of each Button to a separate public ICommand which specifies what action to take in the view model when the button is clicked. I then bind the visibility of the Border control - which contains all of the other controls I mentioned - to a Visibility property.
When I want to show a dialog, I set the MessageBoxMessage to the message I want to show, makes sure the commands are set properly, and then set the Visibility on the Border to Visibility.Visible. This shows the box (border), message, and buttons.
You can even implement a semi-transparent rectangle underneath the border (over the rest of the form) that you set to visible at the same time. This will give you the nice "form dimmed" effect and also block the normal form controls from being clicked. A general note - for this to work, these controls need to be at the very bottom of your XAML as the z-index among controls at the same level is inferred from their placement in the XAML - lower in the code is top level on the form.
Let me know if you have any questions about implementing this if it sounds like what you are looking for.

How can I change the default Control template of a TabControl according to my choice

I am trying to develop a Customize TabControl in which I'll divide the Whole TabControl into three Parts:
1)Tab Header
2)Common Region(for all Tab) and
3)Tab Content region for specific Tab
Update:
Please provide your best answers or samples if you have then, any type of help will be appreciated.
Thanks in Advance
You can overwrite the TabControl Template to be anything you want, including making it have a static region that stays visible regardless of which tab is selected.
Within the Template, I normally use a panel with IsItemsHost=True to define where the "Tab" portion of the tab control will be displayed and <ContentPresenter ContentSource="SelectedContent" /> where I want the selected tab content to be displayed.
The TabControl.ItemTemplate can also be overwritten to further define your Tabs, and TabControl.ItemContainer can be overwritten to modify just the TabContent part of the TabControl.
Hmm ... I don't quite understand why one would do this, but if I were you I would implement this using WPF.
I would implement the tab header as a StackPanel filled with Buttons (their style obviously needs to be redone so that it looks like tabs). The content would be a rectangle containing a grid whose content changes on clicking a button. And that's pretty much it for the basic sceleton. I don't understand your Common Region. What is also nice is to add a little "X" inside each tab in order to close it. That can be done with buttons as well.
It might make sense to use Expression Blend to create such a control.
Best wishes,
Christian

How do I make a single control appear across many tab control pages?

I have a WPF tabcontrol with 3 tabs. On the top of the first page is a scrollviewer with a couple buttons which make up a menu of common tasks (save, load etc). I would like this scroll viewer to appear at the top of every tab. Is it possible to do this without simply copying and pasting the code to every tab?
You can make a custom control that contains the UI and logic for the buttons, and then include that control on each tab. The best way to do this is to create a subclass of ScrollViewer, and in the XAML define each of the buttons. On each of your tab pages you can create and create an instance of your new subclass.
This will result in a different instance of your class on each page, but the logic for the buttons will only exist in the code once.
You could implement the scroll viewer and buttons outside and on top of the tabcontrol.

Categories