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}" />
Related
New to WPF and struggling with the following:
A StatusBarItem displays a simple timer (seconds since window loaded). The binding source property and backing DispatcherTimer are sitting in a Singleton class and are static, which seems reasonable as there is only one StatusBar per application. The class implements INotifyPropertyChanged as it's a one-way binding to the StatusBarItem.
WPF 4.5 can bind to static properties, using static events (see here).
This does not work because INotifyPropertyChanged is not static, so the event cannot be static, so the property cannot be static. The solution is to keep the timer static, but make the property not static.
What is the point of introducing binding to static properties in 4.5 if they cannot rely on INotifyPropertyChanged? Is there a static version of INotifyPropertyChanged? I must be missing something, but don't know what. Thanks for any clarification.
Yes you are missing something, the page you link to describes how you define events that notify WPF that a static property has changed.
For a property named MyProperty, either create an Event as below
public static event EventHandler MyPropertyChanged;
or a generic event called
public static event EventHandler<PropertyChangedEventArgs> StaticPropertyChanged
InotifyPropertyChanged is NOT used for static properties only one of the aboved named events
You do not need to implement INotifyPropertyChanged interface, just declare events like in the documentation.
You could wrap the static property in an instance property, but the design is the true problem here. Why have static properties in a singleton? By definition, there should only be one instance of a singleton in a process, making static properties (other than a static property to hold the instance of the singleton) pointless.
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>
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...
Is it possible to introduce 'custom' attributes into different UI Elements in XAML ? Also to read them later like we add attributes for server controls in ASP.NET ?
I intend to read specific attributes and operate on them together.
It sounds like you're trying to find Attached Properties.
An attached property lets you add in a property, definable in Xaml, which can be "attached" to any UIelement. You then retrieve them in code like any other Dependency Property.
Here is the approach I tend to take with this.
Create a new class file called Meta:-
namespace SilverlightApplication1
{
public static class Meta
{
#region SomeValue
public static string GetSomeValue(DependencyObject obj)
{
return (string)obj.GetValue(SomeValueProperty);
}
public static void SetSomeValue(DependencyObject obj, string value)
{
obj.SetValue(SomeValueProperty, value);
}
public static readonly DependencyProperty SomeValueProperty =
DependencyProperty.RegisterAttached("SomeValue", typeof(string), typeof(Meta),
new PropertyMetadata(null));
#end region
#region SomeOtherValue
// Boilerplate code from above.
#end region
}
}
A value can now be attached in XAML like this:-
<TextBox x:Name="txt" local:Meta.SomeValue="Hello, World!" />
At some point in code this value can be retrieved with:-
string value = Meta.GetSomeValue(txt);
Note you don't have to stick with String as the type of the property you can pretty much use any type you like with the limitation that if you can to attach it in XAML the type must be compatible with the way XAML constructs objects (for example requires a default constructor).
The way I've accomplished that is by creating a new class that inherits the base control.
For example, I have a class called WebTextBox that inherits TextBox. And inside WebTextBox are some custom properties and events. By doing this you're inheriting all the behaviors of the TextBox control. But you can get creative here if you choose, even modifying the behavior by overriding events and such.
Anyway, after you create the class you'll then have to add the namespace for the project to the XAML. Something like this:
xmlns:me="clr-namespace:YourNamespace;assembly=YourAssembly"
And then you can add a WebTextBox (or whatever you call it) like this:
<me:WebTextBox CustomAttribute="cool stuff" />
How do you pass "this" to the constructor for ObjectDataProvider in XAML.
Lets say my presenter class is:
public class ApplicationPresenter(IView view){}
and that my UserControl implements IView.
What do I pass to the ConstructorParameters in the code below so that the UserControl can create the ApplicationPresenter using the default constructor?
<ObjectDataProvider x:Key="ApplicationPresenterDS"
ObjectType="{x:Type Fenix_Presenters:ApplicationPresenter}"
ConstructorParameters="{ ?? what goes here ??}" d:IsDataSource="True" />
I only need to do this so that I can use Blend 2. I know that I can do this in the code behind, but if I do I can't instantiate the class from within Blend. I also know that I can create a parameterless constructor for ApplicationPresenter and pass it a dummy class that implements IView, but I would rather do this in markup if at all possible.
My code behind at the moment is:
public MyUserControl()
{
InitializeComponent();
DataContext = new ApplicationPresenter(this);
}
I'm just starting with Wpf and was under the misapprehension that I should be trying to do everything in XAML. I've just watched a few videos from WindowsClient.net which are starting to clear some things up. But boy is this a complex technology!!!
i don't know if it works, but you could give your user control a name , e.g.
x:Name="myUserCotrol"
and then use it in a binding:
... ConstructorParameters="{Binding ElementName=myUserControl}" ...
this could work
This will be directly supported (if memory serves well) in the next version of XAML as demonstrated by Rob Relyea at this year's PDC.