All ValueConverter examples I have found used Resources to create ValueConverter instance. But my ValueConverter uses some dependencies which are resolved by IoC framework. So I would like to set Binding Converter property to some property of my ViewModel which is accessible through DataContext. I tried to do it but got an exception telling I can't bind anything to Converter property of binding.
You cannot bind Binding.Converter as it is not a dependency property, but you can make the converter inherit from DependencyObject and declare dependency properties on that instead, then you can bind those.
Note however that you might need to jump some hoops to get what you want as you will not be able to bind to the DataContext. You probably cannot use ElementName either because the converter will have no name-scope. One common workaround is to target an object with the right DataContext using Binding.Source with x:Reference. Make sure not to declare to instantiate the converter inside the visual tree of the targeted object or x:Reference will throw cyclical dependency errors (the targeted object's Resources are fine).
Related
I have a main window coupled with a view model.This main window uses a usercontrol which also has its own viewmodel.
What I would like to achieve is setting a binding in the main window's xaml between one of its viewmodel's custom property and one of the usercontrol's viewmodel's custom property.
How would one go about doing that?
Could you instead use the ViewModels as projections of a Model?
That is, could you have a class that holds the state (or actions) that both the VMs need to expose and have both the VMs reference this class?
If for some reason you have to couple views to something outside their own DataContext I believe you can only go up the visual tree by using RelativeSource FindAncestor in the binding. I don't think you can traverse down (e.g. Window -> Control).
If you really want to Bind them together you could make your ViewModel's properties Dependency Properties and your ViewModel derive from DependencyObject - then you could do..
var binding = new Binding("Something");
binding.Source = myViewModel1;
binding.Mode = BindingMode.TwoWay;
BindingOperations.SetBinding(viewModel2,ViewModelType.SomethingProperty,binding);
If this is a good design having your viewmodels derive from DependencyObject is another question..
You could also try looking at this library that allows binding to and from POCOs.
I ended up not using a modelview for my usercontrol, not as neat but at least it works and is less complicated datacontext wise.
Thanks to all.
let say there is a textbox and i want to control the visibility of this control using MVVM, is there a sample on how to do this? First create a dependency property then get it hooked up in the ViewModel. Thanks.
Typically, you wouldn't need to use a dependency property in this case. Dependency properties really only need to be implemented for things like controls themselves, not for determining behavior. Behavior, such as the visibility of an element, can be handled directly via data binding.
Your ViewModel would just have some property, and you'd bind the TextBox.Visibility property directly to the ViewModel property.
The one "sticky point" is that you often will want to have some type of IValueConverter that will convert from your property type to a Visibility enum.
I know how to bind to a DependencyProperty and how to create my own DependencyProperty's. Is there a way to get the value from the DataContext of the control in its code behind using the binding system (i.e. not reflection)? I'm considering a method of binding directly to the code behind rather than having to bind through the interface. I know that I could just create a DependencyProperty and bind to that, but I was just wondering if there was a way using a BindingExpression or something to forgo having to create a DependencyProperty.
I'm using Silverlight 4 with Expression Blend 4 and Visual Studio 2010.
No, the only two ways you could do this are via reflection, or by creating a 'local' dependency property and binding that to your DataContext by some expression. There is no other 'magic' way!
In WPF some properties of controls are dependency properties, others are normal properties.
Eg TextBox.SelectedText is a normal property and not a dependency property. I use MVVM and it happens often to me that I want to bind to some property, but I cant, because it is a normal property.
Can someone explain to me, what logic stands behind the decision whether a property is normal or a dependency property.
Also, can I work around this and somehow bind to the normal properties as if they were dependency properties?
Go through these links
When to use a WPF Dependency Property versus INotifyPropertyChanged
http://social.msdn.microsoft.com/forums/en-US/wpf/thread/65bf126f-e706-4d3e-8cc3-e0130a0ee6de
http://joshsmithonwpf.wordpress.com/2007/06/22/overview-of-dependency-properties-in-wpf/
WPF: What distinguishes a Dependency Property from a regular CLR Property?
How to set bindings on CLR Properties using DataResource
You will get better idea about what you are looking to find out
You can bind to normal properties, but if your property changes, your binding will not get notified. However, you can implement INotifyPropertyChanged in your classes and the binding will update your control automatically.
I have read that SL4 introduces the ability to data bind properties on objects that derive from DependencyObjects, where previously data-binding only worked on FrameworkElements or FrameworkContentElements.
However, I am not clear on how the binding source is determined when binding properties of DependencyObjects.
In the case of FrameworkElements, the element's DataContext property is the source object ('walking up the tree' to find a DataContext, if the DataContext isn't set directly).
In the case of DependencyObjects, I would guess that the DataContext used is the DataContext of the 'containing' FrameworkElement in the XAML file. But what is the mechanism for determining this containing object?
In my particular case, I am trying to bind the property of a DependencyObject that lives in an ObservableCollection that is a property of a FrameworkElement. Unfortunately attempting to bind the property on the DependencyObject fails, as the databinding system appears to be using the DependencyObject itself as its own DataContext. It complains (in the output window) that the type does not have a property with the name specified in the binding expression. Binding a dependency property of a FrameworkElement in the same UserControl with the same binding expression is successful.
Have you tried stating the Source or ElementName property when defining the Binding?
(e.g: {Binding Source={StaticResource theFrameworkElement} Path=theObservableCollection[0]}
or {Binding ElementName=theFrameworkElement Path=theObservableCollection[0]}