Cache ContentControl/ContentPresenter Bound to ViewModel - wpf

If I have a ContentControl/ContentPresenter whose content will be set to a ViewModel - and I have a type-referential data template for the ViewModel - is there a clean, MVVM-compliant way to take a "snapshot" of the ContentControl/ContentPresenter when it has rendered everything?
The idea is that I will have up to three or four ViewModels "open" at any given point in time, and I will have a ListBox bound to a collection of the ViewModels. There will be one ContentControl/ContentPresenter displaying the "current" view model being viewed. If the user moves their mouse over one of the ViewModels in the ListBox, I want to display a scaled down preview of the ViewModel for them. Rather than rendering the content everytime, I want to cache the content for a ViewModel once it has been displayed initially in the main ContentPresenter.
Has anyone seen anything like this before?
Chris

You can create a bitmap of any visual using RenderTargetBitmap. The bitmap will include the entire visual tree of the visual.
http://msdn.microsoft.com/en-us/library/system.windows.media.imaging.rendertargetbitmap.aspx
http://msdn.microsoft.com/en-us/library/aa969775.aspx

Related

WPF TabControl vs. Pages

I am creating a resource-intensive dashboard application that will have many areas of data visualization. I am thinking that it would be best to use a frame and load the pages needed one at a time using WPF pages. These pages will also have different data contexts, security restrictions, etc. But, another developer says I can accomplish the same thing using a TabControl.
Does a TabControl load all the items in all the tabs at once, on application startup? Or, can I load them lazily as needed like with WPF pages (page only loads content when navigated to)? Also, can you have different data contexts per each item in a TabControl?
In WPF you can use UI Virtualization which means that only the visible controls are initialized and rendered. As far as I know, the TabControl does not support UI Virtualization by default but maybe you can add it manually or use another control. Maybe you want to have a look at the following article which presents some performance tips. There is also mentioned that there is a difference between UI and Data Virtualization. Not showing the controls does not mean that the underlying data are not in memory. All your binding targets will be loaded, but the controls will not be rendered.
To your second question: Yes, every TabItem can have its own DataContext. If you use a TabControls ItemsSource to bind a list of items, the DataContext for every TabItem will be one item of the list. If you manually add TabItems, you can set the DataContext like that:
<TabControl>
<TabItem DataContext="{Binding Context1}" />
<TabItem DataContext="{Binding Context2}" />
</TabControl>
It is more complex than you would guess. If you bind to Tab Collection (think MVVM) then the tab only get created when it is selected. And with a Collection if you leave a tab and come back it gets built AGAIN. If you create the tabs in XAML then the tabs are all built when the windows loads. Yes you can have different DataContext for each tab. What I do for lazy loads is bind to the TabItem property IsSelected and if it is false all the Properties in the class just return a (fast) static type compliant value. If IsSelected is changed to true then I load the real values and call NotifyPropertyChanged (and I save the real values).
I use the heck out of this were I load a big objects and one tab is a summary. Tabs do not virtualize but if you have big lists then for sure use virtualization in the tab. You can use BackgroundWorker to create properites but once it returns and you bind that returned value the UI locked until the UI control is rendered. For me reuse of a single frame versus tabs is a UI thing. Just to break up code I typically load a tab with a frame and a page (and I typically pass data to the page in the ctor to load dynamic content).

How to clone a WPF grid with all children?

I have several controls in my WPF window. I have divided the window into 4 sections. If I click on the section in the upper left, I want to copy the contents of this section in another window popup.
What I want to copy is a grid that can contain a lot of different controls, ex: togglebutton, button, panel... derived from ItemsControl, Control... and with DependencyProperty, ObservableCollection etc..
I tried :
XamlWriter.Save(data);
XamlServices.Save(data);
but I always have these errors :
Cannot serialize a generic type 'System.Collections.ObjectModel.ObservableCollection`1 or
A BadImageFormatException has been thrown while parsing the signature. This is likely due to lack of a generic context. Ensure
genericTypeArguments and genericMethodArguments are provided and
contain enough context.
You should not clone XAML itself, this is brutal and wrong way.
Take a look at the possible MVVM solution below. Let's say single DataGird represents a single business Item, so you need following:
ItemsWindow.xaml - represents a ListView and single item of ListView is DataGrid representing an item details
ItemsViewModel - expose list of Item objects (ObservableCollection<Item> Items { get; set; })
Each item of the ItemsWindow.ListView is represented by a DataTemplate like ItemDataTemplate
ItemsViewModel exposed command ICommand CopyItem and in command handler actually copyiing only instance of the Item business entity and adding it to the Items list, WPF reflects this changes via bindings and UI will be updated by a new ListViewItem with a DataGrid representing a details of just copied Item
Useful links:
Model View ViewModel pattern
Commands
Data Templating Overview - short and clean overview of WPF data templates with examples

In WPF (or silverlight or WP7), should a picture wall derive from ItemsControl or Panel?

Assume that I need to create a class called PictureWall, which will be used to show pictures.
Now I find that Panel and ItemsControl can both be used to hold Children elements. So should the class PictureWall derive from Panel? or should it derive from ItemsControl.
Note: This is not a real requirement, it's just a hypothetical question. The real question is: when should I create a subclass of Control (or ItemsControl) and when should I create a subclass of Panel?
Note 2: This imagined picture wall control is not to be used in one application only. It may be used by other developers. If it derives from Panel or ItemsControl, it'll expose the property named Children to other developers. So in this case, deriving from Control is a better idea, right?
Note 3: This imagined picture wall control has its own default way of loading certain pictures (for example, pulling pictures from a server) and it does not want this way to be messed around. If this is the case, then we should not inherit ItemsControl, right?
Panel is a container that is used to arrange its children. For example: Grid with a title and one button on the bottom and an image on center - Grid is very flexible to help you move stuff and arrange them when you change the size of window etc.
ItemsControl is a control that helps you with a collection of items. Let's take a concrete example: Listbox. You can very easly show a list of items, applay template to all of them, so on and so forth.
Control class is basically a UI element that can have its own template.
Note that, it is a way much better to define own UserControl, edit template or style of your PictureWall, insted of subclassing (there are many advantages, for example you can use Blend to redefine the style).
Edit:
# note2
If I were you I would make my own User Control to reuse existing controls to make what I want. If that won't be enough I would subclass Control.
[StyleTypedProperty(Property = "FooStyle", StyleTargetType = typeof(Control))]
public partial class MyDangControl: Control
{
...
# note3
This is a bad idea to combine all in one. You should split the logic that fetch the data form yout Picture Wall. For instance, user presses thumbnail to download the image and whole UI hangs. Horrible UX.
To be crystal clear, let me quote Pro WPF in C# 2010
Control
This is the most common starting
point when building a control
from scratch. It’s the base class for
all user-interactive widgets. The
Control class adds properties for
setting the background and foreground,
as well as the font and alignment of
content. The control class also places
itself into the tab order (through the
IsTabStop property) and introduces the
notion of double-clicking (through the
MouseDoubleClick and
PreviewMouseDoubleClick events). But
most important, the Control class
defines the Template property that
allows its appearance to be swapped
out with a customized element tree for
endless flexibility.
ContentControl
This is the base class for controls
that can display a single piece of
arbitrary content. That content can be
an element or a custom object that’s
used in conjunction with a template.
(The content is set through the
Content property, and an optional
template can be provided in the
ContentTemplate property.) Many
controls wrap a specific, limited type
of content (like a string of text in a
text box). Because these controls
don’t support all elements, they
shouldn’t be defined as content
controls.
ItemsControl
ItemsControl is the base class for
controls that wrap a list of items but
don’t support selection, while
Selector is the more specialized base
class for controls that do support
selection. These classes aren’t often
used to create custom controls,
because the data templating features
of the ListBox, ListView, and TreeView
provide a great deal of flexibility.
Panel
This is the base class for controls
with layout logic. A layout control
can hold multiple children and
arranges them according to specific
layout semantics. Often, panels
include attached properties that can
be set on the children to configure
how the children are arranged.
They both can be used to display elements, but really an ItemsControl offers much more functionality. In addition, an ItemsControl doesn't really display it's elements, it leverages a Panel to do that.
An ItemsControl can display a list of items, which may or may not be UIElements/Visuals. The items can be templated using a configurable DataTemplate, which ultimately determines how the item is displayed. In addition, then items can be bound to an observable collection so it will automatically update.
Neither of these features are supported by a Panel. DataTemplates can be used, but you have to manually create an associated ContentControl/ContentPresenter and add it to your panel.
Ultimately, their functions are different. A Panel is used to display UIElements/Visuals. An ItemsControl is used to display any type of data and apply templates as needed.

Dynamically Load UserControl using MVVM Light Toolkit

I searched this site and i found 2 Links
how to load wpf usercontrol in MVVM pattern
MVVM-Light: Load UserControl into Window
but still i cant find the answer to my problem and this link
MVVM-Light Locator Pattern and Reusable UserControl
i Didn't Understood.... so here is i am stating my problem which might help others struggling like me ......
I have a MainWindow which has 2 parts one has a TreeView(a
UserControl) and the other Displays different user controls(named DisplayPanel).... just like windows Explorer.
The Display Panel on the Right side will display different user controls on Clicking nodes of tree view.
and my TreeView is Itself a user Control.
How can i make this composite UI Work using MVVM. Also I am planning to use MVVM light Toolkit. Does this have something that can help...
An Example will be great
Thanks... :)
Edit
My TreeView in a UserControl I made a dependency property in the UserControl which catches the selected Item fo the tree view so that i can use this dependency property to populate the required view in the "MainView" ContentControl binding....as you advised me in the comments. Everything is till now
Problem is that i want to display data contained in the the selected item and i cannot set the DataContext of the UserControls(which will be displayed in right hand side) to the selected item as then i will not be able to use my view model for the respective usercontrol for commands and other operations
I tried to solve this too.... i used the Mediator (Messenger) in my TreeViewUserControl view model to send a Message to the Usercontrol's(the one that i need to display) view model . Message will be passed whenever the item is selected in the tree view. and message contains the selected node. I forgot to mention i set the datacontext of the UserControl to its view model so that i could display data
But using this approach the problem is that when the I click a type of node for the first time the data is not populated but if the same type of node is clicked again its populated. What’s happening is that UserControls object is availabe when the the tree item is clicked for the first time and Mediator sends the message. So Mediator is not able to pass the message to the userControl view model.....
I totally do not have ne idea to solve this further.... is my way if displaying user control right or I should do something that else....totally confused.....
You could try defining a DataTemplate for each type in the TreeView's ItemsSource and instead of having a specific UserControl on the right side, just bind to the TreeView's SelectedItem. Alternatively, you could use a DataTemplateSelector.
Edited for OP's Edit
Did you do this?
MainWindow has TreeView whose ItemsSource=Binding MainVM.Items.
MainWindow has ContentControl whose Content=Binding TreeView.SelectedItem.
Somewhere in project, have ResourceDictionary where each possible type in MainVM.Items has a DataTemplate defined?
Which ViewModel (MainVM or ItemVM) are you trying to use and why can't you use it?

Silverlight databinding a textBlock in another user control

I have a small usercontrol that basically increments or decrements a value by one. The user control has two buttons(one to add and the other to subtract) and a textBlock that is used to display the value.
I am going to have multiple instance of this usercontrol in another usercontrol so I can manipulate values of a dataclass that has an INotifyPropertyChanged interface. My question is how can I databind the textBlock of the value changing usercontrol to the usercontrol I instansiated it in?
First, I want to state that Silverlight 2 does not support element to element binding. That feature is added in Silverlight 3 (out in Beta now). Having said that, I don't think you want to bind controls together anyway. It sounds like you're trying to build a NumericUpDown control and you probably have some class in code behind that's actually doing the incrementing and decrementing.
If that's the case, you can simply subscribe to the click handlers and call a method on your model like Increment or Decrement. Your model can expose a property for the current value and that property is what is bound to your text box.
Now if you're actually trying to build a NumericUpDown control, you might want to check out the Silverlight Toolkit. The toolkit already includes this control and it also supports data binding.
Check out the NumericUpDown Control here and download the toolkit here.
Finally, binding from a child control to a parent control really isn't any different. The parent UserControl has a DataContext and all child controls inherit that. Each individual child control can also have its DataContext set. Binding expressions are always relative to the DataContext and the DataContext can be set in code. In your case, probably to a model of some sort.
I hope that helps.

Resources