In Flex (AS3) you can do neat things where you bind a property to an expression/variable/object. e.g. button.enabled = {!obj.name.empty} It makes for very neat GUI validation amongst other things.
I don't see any such facility in Visual Studio GUI designer but wondered if this sort of functionality exists in .Net/C# and if so, to what extent?
I don't see any such facility in Visual Studio GUI designer
In fact there is. Select the form/control in the designer, then go to Properties window and expand the (DataBindings) category. You'll see a couple properties there, and more if you click the (Advanced) item.
to what extent?
It could only bind to a property (no expressions are supported). To make it work, the object providing the property must support property change notification. The standard way is implementing INotifyPropertyChanged interface, but there are also other mechanisms - IBindingListimplementation providing ListChanged event, object providing event named PropertyNameChanged for a PropertyName etc.
As I mentioned, standartly you can bind only to properties. However, at runtime with some helpers you can really bind to a method or expression. I've already provided examples of doing that in the following threads Button enable and disable on text changed event, Exchange UserControls on a Form with data-binding, Custom WinForms data binding with converter not working on nullable type (double?), .Net WinForms design to sync Data and Controls for a single item data binding, and my own question Cross tabular data binding in WPF for more complex scenarios.
Contrary to what some WPF-ers say, there are no WF limits when binding to a custom objects/collections. The only limits are when binding to other control properties because control property notification pattern is not strictly followed like in WPF where it is by design.
Related
I don't understand what bindable properties offer more than normal ones. Perhaps the PropertyChanged event? But I guess you could write in your normal property setter whatever you'd write in the PropertyChanged event. One simple example with these two might make it click in my head. Thanks!
from the docs
The purpose of bindable properties is to provide a property system
that supports data binding, styles, templates, and values set through
parent-child relationships. In addition, bindable properties can
provide default values, validation of property values, and callbacks
that monitor property changes.
I am playing with mvvm and wpf. Now, my total solution is MVVM-friendly. The only thing i have put in code behind is the "make new product" & close buttons on the mainview.
Now im adding a menubar, and i was wondering if i can put these "make new product" & close Items in code behind, or is this a no go?
Thanks in advance.
The MVVM way to do it is commands. You can consider them as proxies between your declarative XAML and imperative VM.
Create CreateNewProductCommand, implementing ICommand.
Create a handler for the command performing the actual work as part of ICommand interface implementation (conventionally called On*** - OnCreateNewProductCommand) (you may want to pass paramteres for edit, which is supported by the interface too).
Expose your command as property of your VM.
Bind your menu item command (it'll likely have it, just search for properties containing Command) property to that command using standard binding syntax pointing to the relevant property created at a previous step.
This is not the only way to do it. There're more advanced techniques based on interactions/behaviors etc. Some of them would allow you to bypass command creation and bind your UI element event directly to the executable member of your VM.
Looking for a way to automatically verify, using unit-tests for example, that all controls in my WPF application are set to Binding.
I'm using data-binding to enable localization in the application.
For example, a TextBlock control in a view will have it's Text property binding to 'SomeLocalizedLabel' property in the view model.
I need to make sure that I won't miss any control, and all my 'TextBlock' controls have their 'Text' property bound to something.
I can check the correctness of the localization itself in the ViewModel level.
I can check that all public ViewModel properties are bound to something in the view using the Caliburn.Testability assembly or some other method.
The thing that missing here is to test the View level.
UI testing framework is not the solution I'm looking for, because I wish for something that can be written and executed fast.
See my post here:
How to check if a XAML element supports AutomationId attribute
You could use the mechanisms mentioned to go through all your XAML and look at the Text properties of elements, and decide if they have missed a Binding.
LocBaml is a tool that goes through the XAML to extract localizable properties....your rolling your own localization technique...but I'm sure you can adapt that to extract check "Text" properties and see if they have a binding, then produce a report where they are not.
I am required to use the mvvm pattern. I know that the viewmodel should not care about the view from what I been reading. As a result I don't know how to solve this problem:
I have a dll that basically turns a textbox and listview into an autocomplete control:
SomeDll.InitAutocomplete<string>(TextBox1, ListView1, SomeObservableCollection);
anyways I don't know how to call that method from the viewmodel using the mvvm patter. if I reference the controls in the view I will be braking the rules.
I am new to MVVM pattern and my company requires me to follow it. what will be the most appropriate way of solving this problem?
I know I will be able to solve it by passing the entire view to the viewmodel as a constructor parameter but that will totaly break the mvvm pattern just because I need to reference two controls in the view.
What you're doing here is a pure view concern, so I'd recommend doing it in the view (i.e. the code-behind). The view knows about the VM and its observable collection, so why not let the code behind make this call?
(I'd also recommend seeing if you can get a non-code/XAML API for "SomeDll", but I have no idea how much control you might have over that)
There are two things that I'd point out here -
First, this is effectively all View-layer code. As such, using code behind isn't necessarily a violation of MVVM - you're not bridging that View->ViewModel layer by including some code in the code behind, if necessary.
That being said, this is often handled more elegantly in one of two ways -
You could wrap this functionality into a new control - effectively an AutoCompleteTextBox control. This would allow you to include the "textbox" and "listview" visual elements into the control template, and bind to the completion items within Xaml.
You could turn this into an attached property (or Blend behavior), which would allow you to "attach" it to a text box, and add that functionality (all within xaml). The items collection would then become a binding on the attached property (or behavior).
I'm new to c# and I'm looking for a way to bind a property of an object of my own to the value of a textbox in a regular form (reset the property of the object everytime the value of the input changes).
I have read some information and it seems that this can be done only for database objects. Can you give me additional information.
Assuming you mean Windows Forms textbox,
say
textBox.DataBindings.Add("Text", obj, "SomeProperty");
whenever you feel like binding it. Bindings are usually done in Form_Load event handler, if the object can be obtained at that time of course, and if there's no complex logic with different data sources.
Note that this will only work in one direction (changing TextBox will yield object property changes). To sync the other way round, the object must implement INotifyPropertyChanged interface.
If you want to persist the information between runs of the application (i.e. have it be saved when you close the app and re-appear when it opens), it's easiest to use the Windows Forms designer (I assume you are coding a WinForms app) to bind the value of the TextBox to an application setting. (This article on validation provides a screenshot similar to what you want.) (EDIT: Here is the exceptional article on the subject: Exploring Secrets of Persistent Application Settings. And here is a snippet page that I put together to discuss binding.)
This binding is automatically two-way, unlike the binding that #gaearon mentions. You just need to make sure that you save the settings (i.e. Properties.Settings.Default.Save()) before closing the application (e.g. as the event handler for the Form.Closing event).
If you need more clarification, leave a comment.