I tried to derive from the Selector class cause I need a similar functionality as the ListBox but it is no ListBox.
I had a look at the signature of the Selector class and it is (http://msdn.microsoft.com/en-us/library/system.windows.controls.primitives.selector(v=vs.95).aspx)
public abstract class Selector : ItemsControl,
ISupportInitialize
But the problem is that the constructor is internal. So it is not possible to derive from this class outside the assembly (ListBox and ComboBox are in this assembly).
I now derived from the ListBox to achieve my goal, but my question is:
Why has the selector class an internal Constructor?
Because the Selector class is abstract. You can't create instances of abstract classes, and the easiest way to make sure you can't even do that by mistake (in a regular way) is to not make a constructor available.
I don't see an entry for the constructor on the MSDN, but my bet is that it's probably a protected constructor, not an internal one.
But from what I can see, nothing stops you from deriving from Selector, and create your custom implementation.
Edit:
Reflector shows the constructor to be internal indeed, so no deriving...
Related
I use this code in my views' (which are UserControl) constructors:
this.DataContext = The<Chart1ViewModel>.Instance;
Where the The<> is a generic static per-type singleton storage which also needs to implement INotifyPropertyChanged to notify of it's instance replacement.
I would like to XAML this code like, how can I do that? I have never before did bind anything to a generic static classes.
If I can't, how could I create a Binding to that static class' Instance property?
As far as I know you'll have problems using generics in XAML. Nevertheless you can use static classes. By wrapping a property around the generic you could do something like this:
<Control DataContext="{x:Static The.Chart1ViewModel.Instance}" />
i'm using Prism-MEF-WPF and Sometimes i need view model gets constructed from the XAML
of the view, so the container is not involved and can’t do the dependency injection
automatically (as there is no Export attribute used with VM).so there should be some
class in Prism-WPF like CompositionInitializer to enable me to ask the container to
do the injection.In case there is equivalent class how to use it, and in case there is
no equivalent how to construct view model from xaml of the view knowing that i use MEF.
Thanks in advance.
The problem is that you can't create an object in XAML if it doesn't have a parameterless constructor.
Using the ServiceLocator, you can achieve this. It will work as an IoC (and is set up by Prism/MEF, you just have to drop the .dll):
The xaml:
<Window.DataContext>
<local:ViewModel/>
</Window.DataContext>
The code-behind:
class ViewModel : NotificationObject
{
public ViewModel()
{
var firstDependency = ServiceLocator.Current.GetInstance<FirstDependencyType>();
//... more dependencies here instead of as constructor parameters
}
//class code omitted for brievity
}
Here is the right answer which i got from Agustin Adami "http://blogs.southworks.net/aadami":
Based on my understanding the view model can be instantiated in XAML as the view’s DataContext only if a view model does not have any constructor arguments. And as far as I know creating objects defined in XAML by partnering with an Inverse of Control Container is currently not supported.
Regarding the CompositionInitializer class, as far as I know there is no equivalent class for WPF, on the other hand regarding this topic, I believe you could find the following blog post interesting:
•http://reedcopsey.com/2010/03/26/mef-compositioninitializer-for-wpf/
Also, I believe an alternative for this could be registering the CompositionContainer class like mentioned in this thread:
http://compositewpf.codeplex.com/discussions/311933
As this could let you retrieve this class for example in your view model's constructor, in order to call the SatisfyImportsOnce method to satisfy the Imports defined in the passed class:
this.compositionContainer =ServiceLocator.Current.GetInstance();
this.compositionContainer.SatisfyImportsOnce(this);
Bootstrapper class is what you are looking for. It uses UnityContainer for injecting dependencies. This link here might be of your interest too..
EDIT
If i am getting right, you want to create a ViewModel from your xaml which can be achieved like this(Here local is namespace of your ViewModel class) -
<Window.DataContext>
<local:ViewModel/>
</Window.DataContext>
In Silverlight, if the datacontext of the userControl inn Xaml is Class A, can a button in the bind to a command in a different class (lets say class B)?
Thanks.
Yes this can be done. there are several ways:
You could assign the DataContext of the button to an instance of class B.
You could add an instance of class to the Resources of the button (or one of the button's parents) and point the binding to a StaticResource
Add a property of type B to class A and bind the Command of the button to that property of class A.
I prefer none of these option but if I have to chose I pick #3. #1 and #2 cause too much confusion. The best solution IMHO is to add the Command to class A because that is much more clear and fits a MVVM state of mind
In a word, yes. However, your Class A (datacontext) will need access to an instance of Class B and expose it publicly (or expose whatever it is you want to bind to--like a delegate).
In general, anything you bind to in XAML requires that the binding path is reachable through your DataContext.
I have a class "MyEntity", which does not have a default constructor (well, it does, yet it is not suitable for use).
I have a form with a DataGrid, which has a cool feature for creating new rows.
Problem: DataGrid cannot create new object when no parameterless constructor is defined.
Question: is there a way to provide the DataGrid with a Func that would construct the new object (= some factory method like CreateObject)? Or is there some other solution?
PS Adding a default constructor is not an option - it is not suitable for my purposes. POCO in EF4 requires objects to be created through a context factory class (calling CreateObject).
Wrap your MyEntity class in another class (MyEntityWrapper) with a default constructor and databind the grid to a collection of MyEntityWrapper objects. Put it in a VM to keep you view (the grid) and model (the collection of MyEntity object) clean.
I have a class that needs to notify that something significant has occurred. The class is in a WPF-project, even though this specific class, is lookless (and doesn't inherit from UIElement, neither directly or indirectly).
Normally, I just register a RoutedEvent to get this functionality but as this class neither has AddHandler nor RemoveHandler, I can't get it to work. Anyone knows of another way of get the RoutedEvent behaviour?
As far as I know, if your class isn't a UIElement, it cannot be part of the visual tree, and if it isn't part of the visual tree, you cannot throw RoutedEvents. They're strictly a UI concept.
I think the recommended approach would be to either make your class inherit from UIElement, or if that's not possible/desired, create a counterpart for your class which does inherit from UIElement and use this second class in the visual tree where you would normally place your original class.