How to activate all the parent(i.e. one tab) of a UserControl in WPF - c#

For an error validation mechanism, I've to be able to "navigate" in my application to one specific pane.
Currently I've one "SelectedNode" and tries to focus the control that is bound to this property(basically, I've an AttachedProperty to set the IsFocus, based on the name).
My issue is that sometimes this page contains tabs. And it appears that the control cannot be focused if it's hidden(not in the active tab).
Is there a way from an UserControl to go up in its visual tree to "activate" all his parent?
I cannot just bind the "SelectedIndex" of my tabcontrol in the viewModel, for a lot of reasons:
The UserControl that has the tab has one sub user control for each tab, so the usercontrol doesn't know what is in which usercontrol
Putting such things in the ViewModel is wrong, the ViewModel should not have to know that it's displayed in tabs or all in the same pane
Thanks!

Related

WPF UserControl in Items Control and Popup

I am building an application which has an ItemsControl, and my DataTemplate for this contains a ContentPresenter. I am binding this ContentPresenter to a UserControl property of my ViewModel. I would like to be able to allow the user to click a button on the UserControl which then shows that UserControl in a new popup window, and then when closed, it reverts back to being in the ItemsControl.
I can get this to show the UserControl in the popup window no problems, but have no idea how to revert it back to being in the ItemsControl.
Any suggestions for this approach would be greatly appreciated.
thanks
Put simply you should not keep a UserControl (a UI element) inside your "ViewModel". Your ViewModels should hold the State of your UI and not the UI itself. Define two different UserControls for the two parts you want it in but this time Bind them to the same ViewModel properties.

WPF Design Time 'Page' Switching

I've written a custom WPF menu control (similar to a tab control, I suppose) using a ListView to hold the headers, which then can be clicked to switch to the appropriate page. The problem with this is, when I'm coding up the XAML for windows that use the control I can't see anything past the first page in the designer view.
Is there a way to let me switch the view within the Visual Studio designer?
If your ViewModel has something like a SelectedTab property that is bound to your custom menu control's SelectedItem dependency property, then you could change your design-time data (or your design-time ViewModel) to make that tab selected by changing the SelectedTab property appropriately.

Initialization of tabs in tabControl

I'm using tabControl with many tabs(>10), and each tab has UserContol. But tabs Initialize each control at starting my app. It's making my program too slow. I want to run my userControl only when I click on it. How can I do this?
You should improve your UserControls that they do not do the performance stuff until they get activated/visible. Give them a method Activate which the tabcontrol calls when the tabs becomes the selected tab.
Alternatively you could not add the UserControls to the TabPage content in the designer. Instead create your UserControl when the tab becomes active. But this will make them insivible in the Designer.
You could either have a marker such as IsLoaded and until a tab is selected, not load the controls. When the tab is then selected, if it hasn't already been loaded, you could load the controls and add them at runtime to the tabs Controls.
Or, you could have the controls added but not do anything with them until the tab is selected, and then each tab will do the calculations or whatever and update the correct controls.
It's all about your design.
Add a handler to your UserControl's Load event. Then kick off slow activity in the handler.

Hierarhical navigational model with Caliburn.Micro

I want to achieve the following hierarchical navigation model in a WPF MVVM application:
ShellView
Page1View
Subpage1View
Subpage2View
Subpage3View
Page2View
...
Meaning that on Shellview, I have buttons to activate/deactivate Page1View and Page2View. On Page1View has buttons to activate Subpage1View and Subpage3View and Subpage1View has a button to activate Subpage2View.
Where should the subpages be displayed ? Should the navigational model map to a view structure exacly ? I mean, I have a ContentControl (named ActiveItem) on ShellView, where Page1View and Page2View is displayed when activated. Should the Subpage1View be displayed on Shell's ActiveItem ContentControl or on the parent view (Page1View) ?
If I should display a subpage on ShellView, then how to activate it properly ? Because the button to activate it is on Page1View not on shell.
If I should display a subpage on the parent view (Page1View), then I must create a ActiveItem ContentControl on Page1View and hide it before it's needed. How ? Also I guess parent view scrollbars could become a problem.
Please advise!
Using Caliburn.Micro 1.3, .NET 4, WPF.
Thanks!
I threw a demo together. I'm not sure if it's exactly what you're looking for. The thing to remember is Screens and Conductors can contain Screens and/or Conductors. So nesting or doing complex screen composition is pretty straight forward.
This example has a ShellView with 2 buttons and a ContentControl. Page1ViewModel also has 2 buttons and a ContentControl.
Clicking Page1 or the Page2 button on the ShellView activates the appropriate Page. The same is true once a Page is activated, clicking on SubPage1 or SubPage2 activates the appropriate SubPage.
https://bitbucket.org/dbeattie/cmwpfnavsample/src
We use sub pages this way:
Subpage itself is shown in a ContentControl that is in page view. So we have shell view that has a ContentControl to show active page and in this active page, there is another ContentControl to show Active sub page.
We show list of current page's subpages on the shell view itself (because of layout), so we have ItemsControl that is bound to ActiveItem.Items (provided the ActiveItem is page inherited from Conductor).
So we simply use ItemsControl to choose which page or subpage is the currently active and then use ContentControl to show it. Note that selected subpage is shown through its parent page view.
But basically, it is up to you whether you show subpages directly on shell view (ContentControl bound to ActiveItem.ActiveItem) or use another way.
Although subpages are not included, Coproject sample application might help you. I plan to make it more complex and add subpages later.
ad 2. I don't think you need to hide the ContentControl -if no subpage is selected, it will be empty. Nevertheless, if you want to hide it, I'd suggest using of ValueConverter (from object to Visibility, if object == null then Visibility.Collapsed, otherwise Visible) and bind the ActiveItem ContentControl's Visibility property to ActiveItem again.

Expose multiple command in WPF user control

A better explanation, I hope:
I have a toolbar with 3 buttons on it, all three bound to a Command (including a CommandParameter)
this toolbar is used on several screens
the xaml of the toolbar is exactly the same over all those screens
I want to remove the toolbar instance and replace it with a user control that provides 3 commands, so I can keep the bindings in each screen. The plan is to later change the toolbar functionality, but the external programming interface (namely, 3 commands) is the same.
So:
I created a user control, and created 3 sets of dependency properties for each command (OneCommand, OneCommandParameter, OneCommandTarget) so I can use these for the binding.
I moved the toolbar xaml inside the user control xaml.
I modified the bindings on the toolbar buttons to bind to the intristic user control properties
on each screen (or really, only the first for now) I replaced the original toolbar with the user control,binding the new properties to the correct commands.
The control shows, but the buttons don't work.
That's about it.
--
Original explanation - not so clear:
I have a WPF user control encapsulating a number of buttons. Previously, the control was a Toolbar with a number of buttons on it, but since I need exact the same functionality on a number of screens, I refactored the toolbar into a custom control.
However, I'd like to keep the command bindings of the original buttons.
I created 3 sets of dependency properties (XCommand, XCommandParameter and XCommandTarget) on the usercontrol.
In the user control xaml I bind the "real" buttons to those properties (each button to each set of properties).
Where I use the usercontrol, I bind the new properties to the real command bindings.
In essence, I want to keep the ICommandSource functionality for each "command" that the user control exposes. However, this dual databinding scenario doesn't seem to work, or I'm doing something wrong. :)
Is there a better way to do this? All I need is to "bridge" the commands from outside the control to the inner buttons so the Execute and CanExecute functionality remains.
I solved this. There was a bug in my RelativeSource in the internal control bindings. It works fine as expected, now.

Categories