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

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

Related

How to Detect The Clicked Control of a WPF App Outside the Application

Scenario: I have one more more WPF applications opened. I cannot change the source code of these apps. My purpose is Whenever user clicks any control (button, combobox, textbox etc.) on these apps, I want to know which control / element is clicked, and log it. Simply obtaining the name of the element would be enough like so: "App1 - button3 is clicked." or "App2 - button1 is clicked". If possible, I want to achieve this for both Winforms and WPF apps, but WPF is more important.
Any way to do accomplish this in the background is OK.
I tried examining and using source codes of snoopwpf (Since it is able to detect the element under the mouse cursor by pressing CTRL+SHIFT), but I wasn't able to achieve my purpose. I could not get the elements in different AppDomains. (AppDomainHelper.GetAppDomains() also returns null)
I looked a little into pywinauto module, however couldn't find such a functionality.

Trap keyboard input in a WindowsFormsHost control in WPF

I have an ActiveX control inside a WinForms user control. My WinForms app loves it!
Now, moving over to WPF, I use the user control in a WindowsFormsHost control. Works great..., but I want to treat this control as a single element so the user can neatly hit TAB over the existing WPF controls AND this user control NOT to 'go inside' it. i.e. just treat it as a single control like all the others.
I think what i need is the ability to trap the keys, and in the event handler simply move focus to the next control in the sequence, but I can't seem to trap any keyboard input. Ive tried the WPF PreviewKey.. events and the like, but once the tabbing gets to the control, it seems to stay inside it and WPF events are ignored.
I couldnt find anything on this in many WPF books and the net. Can anyone suggest a way ?
Thanks,
Jack.
Can't you create some sort of a filter by doing a preview mouse down on the panel or window (whatever is the parent of your controls), this way the panel will catch it before the user control and you should set e.handled to true, and if the user control raised the tab event, keep pushing the focus until you get another control. Preview and e.Handled=ture should solve the problem.

Re-using Buttons in 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.

Use popup or dynamic C# winform screen (or other solution)?

On C# winforms project, I have a small table, a filter box, an Add button and a Done button grouped together, and they all fit together within 250x250 pixels. I only need to show these elements to the user when they press a button. I figured this could be done using a pop up modal screen or by making room on the main screen until the user presses the Done button.
I know a disadvantage to modal screens is that they can cause problems for users when/if they lose track of the modal screen and then they think the program's not responding.
The disadvantage I see for using a dynamic main screen is that the reshaping interferes with the overall layout. But maybe I could find a way to overcome that problem.
I'm new to all of this, so I wanted to ask opinions here. Thanks.
Put them all on a panel (or, even better, a custom control), style it to look nice, and then only show the panel on button click.
If you put them in a groupBox or FlowLayout panel possibly in the corner of the main screen and then set the visibility or even enabled on the entire control when they can or can't press the buttons works well.
disable until time they can edit.
groubB.enabled = true;
or
groubB.Visible = true;
When the user clicks the button, could you disable all of the controls in the main form and then place the panel in the center of the main form, on top of the other controls? Maybe make the panel a little larger with some decoration around it for emphasis. Then, when the user clicks 'done,' dismiss the panel and re-enable all of the controls in the main form.

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