WPF - TabItem appears to be data binding content when clicked - wpf

So I have two tab items within a tab. I am data binding on both. If when i click the second tab item the memory goes through the roof. Looks like it is lazy loading or something. So if i don't click it memory stays still, but soon as i click the second tab item it appears to start data binding.
Thanks for the help anybody!

The WPF TabControl unloads the tabs that are not currently visible and reloads them when you switch to them. So changing from Tab1 to Tab2 is unloading Tab1 and loading Tab2 (This can be verified with a Loaded event on an item in the tabs).
Are you doing anything memory-intensive within your tabs or within the Loaded events of any tabcontrols?

Related

How to load content into dynamically created TabItems On-Demand/Lazy Load?

I have a tab control defined in my app and I'm adding tab items to it dynamically. Lets say I have list of objects with lots of data and for each object I show 5-10 tabs. The ordering and the tabs shown differ from object to object. That is why I have to dynamically create these tabs based on my object.
As far as the content inside these tab items go, I generally set the .Content property of each tab to a UserControl and it loads it as shown in below code.
If ContainsFingerprints() Then
Dim fingerprintT As New TabItem
fingerprintT.Content = New FingerprintTab()
fingerprintT.Header = "Fingerprints"
fingerprintT.Name = "tiFingerprintTab"
viewTab.Items.Add(fingerprintT)
End If
Some of these tabs have lots of images and take up lot of memory at run time. That is why I would like to load Content in those tab items 'on-demand', that is, only when the tab is selected.
I could use the SelectionChanged event of the tab control and then load the required content but is there a better solution that WPF offers?

Implementation of menu buttons in WPF/MVVM/Prism

I have a Region in the top left of the WPF application which I want to be my global button bar where the user chooses which screen they want to view, and the appropriate content will then be displayed in the main region. THere will also be a sub navigation region in the top right with sub-menu options within that screen, eg if the user clicked "Maintenance" from the main menu, the sub menu would show options for New, Update, Delete, Edit etc.
What I want to have is a way to create a custom menu bar by simply specifying a list of Text button names, and ICommand/Parameter pairs for the action to invoke on button click. The natural way to do this would be have a MenuButtonViewModel class with dependency properties Title, Command and CommandParameter. One more would also be needed, IsSelected, so there is some visual way for the user to see which screen you are currently on, so it needs to behave like a toggle button, but where only one button in the group can be selected at a time. I can then have a UserControl where the content of the bar binds to an ObservableCollection and uses data templates to render the button bar.
I have tried a few ways of doing this so far but cannot figure out a way that gives me the visual behaviour I want. These are my requirements
1) Button to change background to a different Brush OnMouseOver
2) When button is selected, a different Brush is displayed as the background until a new button in the group is selected, like a togglebutton IsSelected behaviour
3) Only one button in the group can be selected at a time
The ways I have tried to do this so far are
1) Extending RadioButton with my own class, adding dependency properties for command and commandparameter. Setting all controls to have the same group Id. Data template to override display of radio button to make it look like a visual button with triggers for mouseover and isselected.
This works fine, except for one thing. Once you select a radio button in a group, there is no way to deselect all options in the radio button group. So if you navigate to "maintenance" and then click the sub menu for "Countries" for example, then you are displayed the country maintenance screen. If you then go to a different area of the app and select "Deal Entry" from the main menu, you are taken to the deal entry screen. If you then click "Maintenance", it displays the generic "maintenance" content and brings back the sub menu control for maintenance, where "Country" is selected in the radio button group, but this is undesirable. When you navigate back to Maintenance, it should deselect all sub menu options, display the generic maintenance landing page content and let you select a sub menu option before displaying that screens content. The first time you load Maintenance, nothing is selected, but once you have chosen an option, then there is no way to have nothing selected again when you reload the maintenance screen.
2) I then tried extending a ListBox, styling it with a horizontal stackpanel for the content, each listboxitem is a menubuttonViewModel. This allows me to only select a single option at a time and to clear the selection when you navigate away from the page. It also lets me change the background when you mouse over each listboxitem.
The bit I can't get working with the listbox is to have the background different on the IsSelected trigger. There seems to be some trigger on the default ListBoxItem template that overrides anything you specify in CSS so no matter what trigger I put on the listboxitem or menubuttonviewmodel style, the background simply does not change. I think I need to redefine the data and content template for listboxitem, but only have it apply for this listbox, so it can't be a global template change to all listboxitems - as I want to be able to use listboxes elsewhere in the app without inheriting this behaviour.
Can anyone give their thoughts on these two approaches and how to perhaps solve the issues I'm having to make one of them work in the way I want, particularly if you can advise how I can override the content/data template for the listboxitems in my style so they do not use the default triggers, and I can get the IsSelected trigger working for me?
If I understood correctly, you would want to clear the selection on the RadioButtons from the Sub-Menu each time you navigate back from another Main Menu option.
In order to solve this, you could have every Radio Button "unchecked" by manually setting the RadioButton´s IsChecked property value to false. A possible solution could be the following:
If you want to clear any checked option from the Sub-Menu everytime you navigate to a different section of the Main Menu, you could first notify to the Sub-Menu´s ViewModel by publishing an event using the EventAggregator after selecting and clicking a different MainMenu button, from the SelectionChangedEventHandler method of the Main Menu´s ViewModel.
Therefore, the Sub-Menu EventHandler of the published event would update each RadioButton´s IsChecked property value to false, which it could be accomplished by using a "Two-Way" Binding between each IsChecked property defined on the View, and each boolean "RadioButton1IsChecked" property implemented on the ViewModel.
I hope this helped you.

Weird grid behavior ( must click scroll bar )

I'm programming an application that loads a list of rows in a grid using WPF, DevExpress with an MVVM approach.
I have a button that adds an new element to a ListRow, which is binded to GridControl's ItemSource. This list notifies the view when it's modified.
The problem is that when you click NewRow button the scroll bar grows bigger with each click but only when you click in the scroll bar the content of the list is repainted in the window.
Does anyone have any idea? Why is this happening?
PS: I didn't write here any code because I think is needless to show such a long code. But I can put it if you want.

Silverlight 4 - Instantiate Objects Not Yet Visible

I have a Silverlight 4 application that uses multiple tabs. On Tab 1 I allow the user to select items from a grid, right click and select a context menu item that sends the items to a different grid on Tab 2. However, if the user has not clicked on Tab 2 yet, the grid I am trying to add items to does not exist yet.
What is the most elegant way to make sure that all objects on the second grid are instantiated even if the user has not selected the tab yet?
Thanks,
-Scott
Assuming you are adding items to data grid of some sort then the way to go is to add your items to an ObservableCollection and then data bind the grid to the collection.
That way you don't have to worry about whether the grid is actually visible or not and it will update itself when it does become visible.
Instead of trying to prepopulate the grid on tab2 with data that does not yet exist, you could use the MVVM pattern.
Tab1 could be view1 of viewModel1. Tab2 could be view2 for viewModel2. When view1 updates viewModel1, ViewModel1 updates ViewModel2, which, in turn, updates view2. Then, you only need to set bindings for the visibility, isEnabled, and ItemsSource properties.

Frame Navigation in Silverlight app

I have a WebRibbon control for a menu. I have a TabControl on the page that holds all content. As you click on a menu I create a TabItem, add a frame to the tab item, navigate to a specific xaml page in that frame and then add the frame to the tab item. It works fine for the first page. The content of the xaml page displays correctly in the frame. A problem arise as soon as I navigate to a second and third page. All the frames on all the tab items navigate to the last selected menu items corresponding xaml content.
frame1.JournalOwnership = JournalOwnership.OwnsJournal;
This solved my problem...

Resources