WPF & MEF UserControl part of a larger view (In design mode) - wpf

I want this usercontrol to be Created via MEF but also to have the usercontrol positioned and properties set on it in Blend... How can I force the composition of the imports that the usercontrol requires when I have a 'Concrete' reference to the usercontrol ?
What happended to PartsInitializer.Satisfy ?
Export for the UserControl
[Export(typeof(IWOFlyOutFilterMenuView))]
public partial class FlyoutTab_WOsViewFilter : UserControl,IWOFlyOutFilterMenuView,IPartImportsSatisfiedNotification
No Imports in the Containing view as it is a concrete instance.
Thanks
Greg

You can force the composition when you have an instance like this:
Import the namespace System.ComponentModel.Composition (to include the extension method SatisfyImportsOnce
Create a CompositionContainer
Call SatisfyImportsOnce on the composition Container and supply it the instance of the usercontrol.
Take care,
Martin

Related

Trying to get a WPF UserControl to Inherit a Class

I have four UserControls in my WPF Application - e.g.
VisualA, VisualB, VisualC, VisualD
I want each of them need to inherit a generic "Player" Class which contains a heap of shared code -. e.g. methods, timers etc
So far this is what I have tried in my Control's XAML
<UserControl x:Class="VisualA"
And here is what I have in a separate Class file.
Partial Public Class VisualA
Inherits Player
End Class
Public Class Player
Inherits UserControl
End Class
In my Window, I'm referencing the UserControl as normal:
<local:VisualA></local:VisualA>
But, I'm getting the following error:
Base class 'System.Windows.Controls.UserControl' specified for class
'VisualA' cannot be different from the base class 'Player' of one of
its other partial types
What am I doing wrong?
I was also under the impression any code (i.e. methods) inside the inherited class (Player) would be able to access the Controls in the UserControl by referencing by name - is that correct?
The base class in the XAML is still set to UserControl. Change it to Player. Also note that the namespace for the Player type will have to be defined. i.e:
<BaseClasses:Player x:Class="VisualA"
xmlns:BaseClasses="clr-namespace:MyProject.BaseClasses"
... all your other namespaces used

Export an MVVM View using MEF

The application that I'm building has several (10+) [Module] executables.
I would like to specify something like this in [Module]View.xaml file.
<Window ...
mef:ExportView ExpectingViewModel={x:Type [Module]ViewModel}
>
</Window>
where mef:ExportView is a MarkupExtension which prepares the parts for the MEF composer.
Currently,...
In [Module]View.xaml.cs file I mark my View with an ExportView(typeof([Module]ViewModel)) attribute inside the . (The typeof param specifies the expected ViewModel.)
I also mark my ViewModel with an [ExportViewModel] attribute.
(Both attributes derive from ExportAttribute.)
Each executable's Application class also inherits from an ApplicationBase class which does the MEF composition and marries/links the View/ViewModel.
Now, I would like to be able to remove all code-behind from the [Module]View.xaml.cs to prevent myself and my co-workers from forgetting to markup the code-behind file.
I'm thinking this would require a custom MarkupExtension. I'm familiar with the basics of MEF, as well as creating custom export attributes with metadata. However, I think this solution would require deriving from some of the MEF primitives.

How to I connect a ViewModel to a View when the view model has parameters in the constructor?

I'm using Prism and Unity to rewrite a WPF application using the MVVM pattern. Most of the Views are connected to the VM via the DataContext property, like so:
<UserControl.DataContext>
<VM:RibbonViewModel/>
</UserControl.DataContext>
The problem is that this method will not work when there is a parameter in the ViewModel's constructor.
public RibbonViewModel(IEventAggregator eventAggregator)
{
this.eventAggregator = eventAggregator;
}
I get the error:
Type 'RibbonViewModel' is not usable as an object element because it is not public or does not define a public parameterless constructor or a type converter.
How do I connect the VM to the View when a parameter is there?
You might have a look at the sample applications of the WPF Application Framework (WAF). In these examples the IoC Container (in your case its Unity) is responsible to create the ViewModels. This way a ViewModel can have constructor parameters. The IoC Container is also responsible to bring the View with the ViewModel together. Maybe this is an option for your ViewModel design as well.
Consider using a ViewModelLocator. Rather than binding the datacontext to the ViewModel in your case above, you bind to a locator which knows how to resolve the ViewModel from the (unity) container and in the process inject any dependencies into the constructor. There's a blog posting summarizes an implementation by John Papa and Glenn Block (one of the people behind prism).
I believe the EventAggregator is registered with the container by default, so it should be auto-wired with the VM when you resolve the VM from the container.
I should mention the code from the above blog is using MEF. This blog I believe has a codeplex example using unity
I don't use unity or prism. But, why can't you just do this:
userControl.DataContext = ribbonViewModelInstance;
You can have a dependency property on the user control which is set. On setting of value of this dependency property, you can set the datacontext.

WPF: How to reuse controls when ObservableCollection is concrete?

I have a object that inherits from TabItem. I have a bunch of Database objects that will reuse the same code so I wanted only one TabItem class and then use DataTemplates to control how each object gets presented.
Problem is that the TabItem shows a collection of Objects and ObservableCollection is concrete.
I've pondered a few solutions but none of them work. Seems like I will have to create one class for each object type even when they will all be the same (except for ObservableCollection having different types). That's not very DRY.
I can't make a UserControl generic, I can't let the UserControl constructor take in a generic class ( unless I define the Type wihc I don't wan't to do). I guess creating a base UserControl class and then inheriting that will have to do. Does it inherit the XAML code as well or will I have to rely on styles and templates?
Am I missing something?
Look into using DataTemplateSelector to provide flexibily in how you present your data in WPF.
Here are three sites that helped me:
Data Templating Overview
How to use DataTemplateSelector to alter views of objects in a ListBox
WPF Tutorial - How To Use A DataTemplateSelector

Creating container usercontrol with design time support in WPF?

I want to create a container usercontrol with design time support in WPF ? How can I do ?
If you want to have a container user control with design time support, you can create a ContentControl http://msdn.microsoft.com/en-us/library/system.windows.controls.contentcontrol.aspx to host other user controls inside your container control.
You can expose the content control as a public property and then assign any other user controls you create to this property to display within the ContentControl. You would have the design time support of the parent control or any child user controls by default since they would all just be user controls.
Declare the following in your UserControl at the top:
[Designer("System.Windows.Forms.Design.ParentControlDesigner, System.Design", typeof(IDesigner))]
public partial class ExpanderControl : UserControl
Required NameSpaces:
using System.ComponentModel;
using System.ComponentModel.Design;

Resources