I want to bind Validation.HasError to a boolean property on my viewmodel. I only want to know when there is a validation error so that I can disable my buttons on my VM using canexecute methods from my relay commands. I get this error though:
'Validation.HasError' property is read-only and cannot be set from markup.
I have tried different modes and they all cause this error. How do I do this? It shouldnt be this difficult.
You can't set a Binding on a read-only dependency property. This is a known bug at Microsoft Connect. You might vote for it.
The BookLibrary sample application of the WPF Application Framework (WAF) shows how to listen to the Validation.HasError property and disable some buttons.
Related
I am building a WPF User Control in VB.net. This control is bound to a dependency property in the xaml.vb code behind.
There is no ViewModel.
This is a control that is intended to be used by dropping it on other WPF forms. What is the best approach to raise an event when the dependency property changes.
Do I need to make the code behind implement INotifyPropertyChanged or simply raise a custom event?
I guess I'm really asking about best practices.
I've just discovered recently the source of a problem that has been occurring within an application that I've been working on that uses traditional WinForms MDI with WPF UserControls hosted within ElementHosts for each form. The problem is that if the user changes the text in a TextBox that is TwoWay bound to a ViewModel property that the setter on the property is not set when clicking on the Save button in the toolbar. The consequence of this behavior is that the last text setting that was set is not being saved.
I'm using the default UpdateSourceTrigger value of LostFocus so I believe what is happening is that the the WPF TextBox element is not loosing focus when I click on the WinForms based Save button in the toolbar. So now that I understand the issue I'm curious what the correct or best way is to solve this problem.
The two main options that I can think of either require manual prompting to update the setter without causing the control to loose focus or just make the TextBox loose focus so that the normal source updating occurs.
You can change UpdateSourceTrigger to PropertyChanged, then the "Save" button will work nicely.
I think your reasoning is correct. WPF and Winforms have both seperate "Focusmanagers", so if you go from WPF TextBox to winforms button, then focus has nowhere to go.
I'm using the binding RelativeSource with the FindAncestor Mode but the binding is not working. How do I debug and see if It is able to find the ancestor?
use Snoop
EDIT: you can of course use the usual debugging mechanisms, but I like Snoop the best. You can navigate to your control and if your binding failed it tells you so
Or you can set PresentationTraceSources.TraceLevel on the binding.
If you are using VS2010 remember to set the Data binding value in Options->Debugging->Output Window
I use WPF Inspector, it's a nice free tools for debugging WPF application in XAML level.
My client is trying to hook to a usercontrols Loaded Event in the View Model. Basically they want to know when the controls loaded event triggers inside the view model. They are looking for a way to do it without code behind the xaml. Is this even feasible. I am looking into whether I can route the loaded event to the viewmodel in the xaml.
One way of doing it is to use InvokeDataCommand. You'd specify the trigger's EventName as Loaded, and then your command (defined in your VM) would execute when Loaded event is fired.
You need to look into commanding. Silverlight support is fairly weak compared to WPF but it does contain the ICommand interface. You can extend controls to give them command properties or implement them via an attached property. The commands basically invoke themselves once an action has occurred in the UI. They are totally independent of how the UI is built (or at least they should be) and so can be completely unit tested.
To my understanding, binding errors are displayed only during debug mode in Visual Studio's Output window. However, I want to know about broken bindings when the user runs my app, and I want to notify him that something is not working quite right.
Is there a way to handle binding exceptions from code, when the datacontext is set, and some bindings are broken?
It's possible to catch all Silverlight binding exceptions in all property setters. See here.