How to validate inputs when click button in silverlight? - silverlight

Im using DataAnnotation to validate input controls. But ValidatesOnExceptions only works when user type something in textbox and press Tab. (Basically on Lostfocus event).
but if user never enters anything in the textbox and click submit. It does not work. Like ASP.NET Page.IsValid property is there any property or method in Silverlight that i can use, that will validates all the controls on UI?

Taking help from the URL provided by Terence, i have prepared the solution below for you.
This you can use to make sure that all the properties are set before service call.
public class PersonViewModel : EntityBase
{
private readonly RelayCommand saveCommand;
public PersonViewModel(IServiceAgent serviceAgent)
{
saveCommand = new RelayCommand(Save) { IsEnabled = true };
}
public RelayCommand SaveCommand // Binded with SaveButton
{
get { return saveCommand; }
}
public String Name // Binded with NameTextBox
{
get
{
return name;
}
set
{
name = value;
PropertyChangedHandler("Name");
ValidateName("Name", value);
}
}
public Int32 Age // Binded with AgeTextBox
{
get
{
return age;
}
set
{
age = value;
PropertyChangedHandler("Age");
ValidateAge("Age", value);
}
}
private void ValidateName(string propertyName, String value)
{
ClearErrorFromProperty(propertyName);
if (/*SOME CONDITION*/)
AddErrorForProperty(propertyName, "/*NAME ERROR MESSAGE*/");
}
private void ValidateAge(string propertyName, Int32 value)
{
ClearErrorFromProperty(propertyName);
if (/*SOME CONDITION*/)
AddErrorForProperty(propertyName, "/*AGE ERROR MESSAGE*/");
}
public void Save()
{
ValidateName("Name", name);
ValidateAge("Age", age);
if (!HasErrors)
{
//SAVE CALL TO SERVICE
}
}
}

I don't think, that there is a way to validate ALL UserControls which are visible on the page. But I would recommend you to have a look at the INotifyDataErrorInfo. This is, in my opinion, the best way to validate data in silverlight. With the INotifyDataErrorInfo approach you don't have to make changes in the view (like ValidatesOnException, ...) and you are able to validate against a WebService in an easy way (This is not possible with data annotations).
Have a look here: http://mtaulty.com/CommunityServer/blogs/mike_taultys_blog/archive/2009/11/18/silverlight-4-rough-notes-binding-with-inotifydataerrorinfo.aspx
Hope this helps you.

Related

Databinding not refreshed in Xamarin (WPF)

I have some trouble with the data binding while using MVVM in Xamarin. Let me explain my architecture:
I have a Manager-class, which contains a ObservableCollection with classes of type t, t is my model class. The Manager-class contains also a attribute called activeT, which is the current selected object of my model class. There are 2 UIs, one which shows the current data of t. The viewModel is bound to the attribute t of my Manager-class like that:
public t CurrentT
{
get
{
return _mgr.CurrentT;
}
set
{
_mgr.CurrentT = value;
OnPropertyChanged();
}
}
_mgr is my singleton Manager-Object.
Now there is the other view, which is able to choose the current t out of a combobox. The viewModel of that view is bound to the ObservableCollection of the manager. If I change the selected object, I do it like with the same code like above. The Property of the manager is the following code:
public t CurrentT
{
get
{
return _currentT;
}
set
{
_currentT= value;
OnPropertyChanged());
}
}
The problem is now, that the first view to view the current selected t does not refresh, though I can see in the debugger, that the current t is changed by the other view.
Can someone help me?
Edit:
I provide some more Code:
The Manager-Class:
public class Manager : INotifyPropertyChanged
{
private t _currentConstructionSite;
private ObservableCollection<t> _constructionSites = null;
public event PropertyChangedEventHandler PropertyChanged;
public void OnPropertyChanged(string name)
{
if (this.PropertyChanged != null)
this.PropertyChanged(this, new PropertyChangedEventArgs(name));
}
public t CurrentConstructionSite
{
get
{
return _currentConstructionSite;
}
set
{
_currentConstructionSite = value;
OnPropertyChanged("CurrentConstructionSite");
}
}
public ObservableCollection<t> ConstructionSites
{
get
{
return _constructionSites;
}
set
{
_constructionSites = value;
}
}
private Manager()
{
ConstructionSites = DataRepository.GenConstructionSites();
_currentConstructionSite = ConstructionSites[0];
}
}
The ViewModels Class A (This is the viewmodel of the view, which shows some data):
public class DashboardViewModel : ViewModelBase
{
private Manager _mgr;
public t CurrentConstructionSite
{
get
{
return _mgr.CurrentConstructionSite;
}
set
{
_mgr.CurrentConstructionSite = value;
OnPropertyChanged();
}
}
public DashboardViewModel()
{
_mgr = Manager.getInstance();
}
}
The View A to show some data:
Binding Setup from XAML:
<ContentPage.BindingContext>
<local:DashboardViewModel x:Name="viewModel"/>
</ContentPage.BindingContext>
Binding a Label to show data:
<Label Text="{Binding CurrentConstructionSite.ConstructionSiteName, Mode=TwoWay}" HorizontalOptions="Center" Font="Bold" FontSize="Large"/>
ViewModel B to choose the current t:
public class ChooseConstructionSiteViewModel : ViewModelBase
{
Manager _mgr = null;
public ObservableCollection<t> ConstructionSites
{
get
{
return _mgr.ConstructionSites;
}
}
public t CurrentConstructionSite
{
get
{
return _mgr.CurrentConstructionSite;
}
set
{
_mgr.CurrentConstructionSite = value;
OnPropertyChanged();
}
}
public ChooseConstructionSiteViewModel()
{
_mgr = Manager.getInstance();
}
}
The View to choose the current t:
<combobox:SfComboBox x:Name="combobox" Grid.Row="0" Grid.Column="1" Margin="8,0,20,0" VerticalOptions="Center" HeightRequest="40" DataSource="{Binding ConstructionSites}" DisplayMemberPath="ConstructionSiteName" SelectionChanged="Handle_SelectionChanged"/>
And if the selection from the combobox changed:
void Handle_SelectionChanged(object sender, Syncfusion.XForms.ComboBox.SelectionChangedEventArgs e)
{
t selectedItem = e.Value as t;
_viewModel.CurrentConstructionSite = selectedItem;
}
The two views are contained as contetPages in a tabbedPage. It works in general, but the changing the selected t in the view B does not update the data in view A. I can see in the debugger that the value of t is changed via view B but when I go back to view A there is the old value. In the debugger I can see that the value is updated.
BTW: ViewModelBase is the class which implements INotifyPropertyChanged
From you second viewmodel, you need to "notify" the first viewmodel that the data has changed.
One of the ways to do that, would be to use a Messenger (MvvmLight has one, so does MvvmCross). You can then use the messenger from your Manager, to notify all the viewmodels that need the info, that CurrentT changed.
In the messenger subscription in your viewmodels, simply call a RaiseNotifyPropertyChanged of your property, and you should be good to go

WinForms binding Generic List - checkable business object to a Grid

We all like how easy it is to bind with WPF. Now I am back working with Winforms and I am looking for a nice way to bind my grid to a List of Checkable of BusinessObject (I am sticking with BindingList for Winforms). So I am essentially just adding a checkable to my business object.
I am using a grid as there will be multiple columns where the user would edit (in this scenario Name and Description on the business object) - as well as adding new objects to the grid and removing from it. Checked list box does not fit for this purpose as I want to edit columns.
For this I am using .NET 4.
I basically want to reduce the amount of UI code in the scenario so I am using a view model based approach which will populate the list. I want the user to be able to check a box alongside each of the business object properties.
Sure I can use inheritance, but if I want to apply the same mechanism against a lot of business objects (having lots of different screens where you check items in a list for the different business objects). Maybe this would be the way to go - but I have my doubts.
Now depending upon the choice of grid - I am using Infragistics - the functionality would hopefully be pretty similar conceptually.
I thought about wrapping the business object up in a Checkable generic class:
using System;
using System.Collections.Generic;
public class Checkable<T> : ModelBase
{
public Checkable(T value)
{
_value = value;
}
private T _value;
public T Value
{
get
{
return _value;
}
set
{
if (!EqualityComparer<T>.Default.Equals(_value, value))
{
_value = value;
OnPropertyChanged("Value");
}
}
}
private bool _checked;
public bool Checked
{
get { return _checked; }
set
{
if (_checked != value)
{
_checked = value;
OnPropertyChanged("Checked");
}
}
}
}
I have made up a business object for this scenario:
public class BusinessObject : ModelBase
{
public BusinessObject()
{
}
public BusinessObject(RepairType repairType)
{
_name = repairType.Name;
_id = repairType.Id;
}
private string _name;
public string Name
{
get { return _name; }
set
{
if (_name != value)
{
_name = value;
OnPropertyChanged("Name");
}
}
}
private string _description;
public string Description
{
get { return _description; }
set
{
if (description != value)
{
description = value;
OnPropertyChanged("Description");
}
}
}
private int _id;
public int Id
{
get { return _id; }
set
{
if (_id != value)
{
_id = value;
OnPropertyChanged("Id");
}
}
}
}
Where ModelBase just implements the INotifyPropertyChanged:
public abstract class ModelBase : INotifyPropertyChanged, IDisposable
{
public event PropertyChangedEventHandler PropertyChanged;
protected virtual void OnPropertyChanged(string propertyName)
{
PropertyChangedEventHandler handler = PropertyChanged;
if (handler != null)
handler(this, new PropertyChangedEventArgs(propertyName));
}
protected bool SetProperty<T>(ref T field, T value, string propertyName = null)
{
if (object.Equals(field, value)) { return false; }
field = value;
OnPropertyChanged(propertyName);
return true;
}
public void Dispose()
{
Dispose(true);
GC.SuppressFinalize(this);
}
public virtual void Dispose(bool disposing)
{
if (disposing)
{
PropertyChanged = null;
}
}
}
So potentially for my grid datasource I would define:
// in view model
var datasource = new BindingList<Checkable<BusinessObject>>();
... populate list
grid.DataSource = viewmodel.DataSource;
So of course my scenario fails at the minute as Value is the BusinessObject reference which has the properties I want to bind to, and Checked is the property for a checkbox which I also want to bind to.
I am trying to kick start the old grey matter with some ideas on this. I don't really like writing code to define grid columns. However, the Infragistics grid has been ok for data binding directly to the BusinessObject at design time. Its possible to add an unbound column (checkbox for my scenario) and handle the checking/unchecking of items manually (which I might potentially have to do).
I am wondering if I am missing any neat tricks with Winform binding of late having missed out with Linq and Entity Framework when they appeared many years ago.

Binding ComboBox and ObservableCollection<KeyValue> in wpf

in My OpenViewModel i collect data:
private ObservableCollection<KeyValue> availableData;
public ObservableCollection<KeyValue> AvailableDatas
{
get { return availableData; }
set
{
if (value != availableData)
{
availableData= value;
NotifyOfPropertyChange("AvailableDatas");
}
}
}
method for collecting data:
public ObservableCollection<KeyValue> CollectData()
{
ConnectorClient client = null;
try
{
client = webservice.GetClient();
AvailableDatas = client.GetDatas();
client.Close();
}
catch (Exception ex)
{
webservice.HandleException(ex, client);
}
return AvailableDatas;
}
How to call the method CollectData in wpf and fill my COmboBox?
thx
You might simply call the method the first time the AvailableDatas property is accessed (e.g. from a binding in XAML):
private ObservableCollection<KeyValue> availableData;
public ObservableCollection<KeyValue> AvailableDatas
{
get
{
if (availableData == null)
{
availableData = CollectData();
}
return availableData;
}
set
{
if (value != availableData)
{
availableData = value;
NotifyOfPropertyChange("AvailableDatas");
}
}
}
Then you should change the CollectData method in a way that is does not also set the property:
public ObservableCollection<KeyValue> CollectData()
{
ConnectorClient client = null;
ObservableCollection<KeyValue> data = null;
try
{
client = webservice.GetClient();
data = client.GetDatas();
client.Close();
}
catch (Exception ex)
{
webservice.HandleException(ex, client);
}
return data;
}
You could override the OnActivated() event assuming you are using an IScreen implementation and load data in there, or just do it in the constructor or a custom Initialise method if you want to roll your own (or in the property accessor as someone has already said).
You can also use coroutines if you want some visual context for the user and a better tie in with CM actions
There is a nice simple implementation of a Loader class here which helps provide visual context to the user:
https://caliburnmicro.codeplex.com/wikipage?title=IResult%20and%20Coroutines&referringTitle=Documentation
This searches the visual tree for a BusyIndicator control and activates it whilst the content is loading e.g. ...
public class SomeViewModel : Screen
{
protected override void OnActivate()
{
RefreshData();
}
public void RefreshData()
{
Coroutine.BeginExecute(LoadData(), new ActionExecutionContext() { Target = this });
}
public IEnumerable<IResult> LoadData()
{
yield return Loader.Show("Loading Data...");
yield return new LoadSomeDataRoutine(client.GetDatas);
yield return Loader.Hide();
}
}
The reason to have a RefreshData method is that this also allows you to bind CM actions and allows the coroutine can grab more contextual information.
Obviously you have less need to worry about the async->sync benefits this gives in Silverlight because you are using WPF (but it still applies to async web service calls), however it still has many benefits and it also helps you to write reusable routines which become part of your application framework (e.g. some level of error handling/logging encapsulated in the IResult implementation etc)
You also mentioned filling the combobox... all you would need to do in CM is place a combobox on your control, and set it's Name property to the name of the property on your VM:
public class SomeViewModel : Screen
{
public ObservableCollection<MyObject> MyProperty { //blah blah... }
}
<UserControl .... blah>
<ComboBox x:Name="MyProperty" />
</UserControl>
This will fill the combobox with the items. You will still need to set the binding for SelectedItem/SelectedValue
I assume you know this already though - if not CM has some decent documentation:
https://caliburnmicro.codeplex.com/wikipage?title=Basic%20Configuration%2c%20Actions%20and%20Conventions&referringTitle=Documentation

Validation Error messages in silverlight MVVM

I'm attempting to implement the technique for data validation from Josh Smith's example here: Using a viewmodel to provide meaningful validation...
My code is remarkably similar to the example, except for a few difference, namely I'm using the MVVM-Light toolkit, and my model person class is a partial class that comes from a WCF backend.
Here's an example of the code in question:
First is the Automatically generated version of the class which comes from the WCF:
public partial class Person : BaseObject
{
private string FooField;
public string Foo {
get {
return this.FooField;
}
set {
if ((object.ReferenceEquals(this.FooField, value) != true)) {
this.FooField = value;
this.RaisePropertyChanged("Foo");
}
}
}
I then extend the partial class to implement IDataErrorInfo:
public partial class Person : IDataErrorInfo
{
public string Error
{
get { return null;}
}
public string this[string propertyName]
{
if (propertyName == "Foo")
{
//Do some backend Validation
}
}
}
And lastly I have a viewmodel:
public class PersonViewModel : INotifyProperyChanged, IDataErrorInfo
{
private string _fooString;
private Person _person;
...
public string Foo {
get { return _fooString; }
set
{
if (value == _fooString;)
return;
_fooString = value;
RaisePropertyChanged("Foo");
}
public string this[string propertyName]
{
if (propertyName == "Foo")
{
string msg = Validate(Foo); //Frontend Validation, range, format, etc.
if(msg ! = null)
return msg;
_person.Foo = Foo;
}
}
}
So when I bind to the property in the viewmodel the validation code defined in IDataErrorInfo's indexer gets executed on the view model and my textbox or whatever gets highlighted if my validation fails, as expected. However in my code the Indexer on the MODEL side never gets executed, at all. I can honestly say that I don't see or understand the mechanism that is supposed to invoke it. I have run the example code from Josh Smith's example, and it does work, calling the MV's this[], then if validation passes the Model's this[] hits for additional validation, but I for the life of me can't see how it happens.
I really hope this is something simple I'm overlooking. Thanks for looking at it.
You are missing
return _person[propertyName];
from the the indexer in the ViewModel.

Silverlight 3 Validation - manual validate not working?

Apologies for cross posting (I asked this on the Silverlight Forum but got no response)
I have an entity that I am trying to use validation on so I have decorated a property like so:
[Required]
[StringLength(10)]
public string Code
{
get
{
return this.code;
}
set
{
if (this.code != value)
{
this.code = value;
this.SendPropertyChanged("Code");
}
}
}
I have a list of these objects bound to a grid. If I put an empty entry in, it shows a validation error. If I put too long a code in, I get a validation error. Perfect! Except...
I want to be able to stop the user from saving the entity so I added the following to my entity:
public bool IsValid()
{
try
{
this.Validate();
}
catch
{
return false;
}
return true;
}
public void Validate()
{
var ctx = new ValidationContext(this, null, null);
Validator.ValidateObject(this, ctx);
}
And when i go to save I call the IsValid method on each object and not save if it's false.
This works fine for the required attribute (it wont save if Code is empty) but not for StringLength (I can save with any length code).
I have reproduced this in a simple project here:
http://walkersretreat.co.nz/files/Slvalidation.zip
Can anyone help?
Thanks!
Mark
you should write as:
[CustomValidation( typeof( MyExtraClassValidation ), "Validate" )]
public class MyExtraClass : Entity, IEditableObject, INotifyPropertyChanged
{
/****/
}
public class MyExtraClassValidation
{
public MyExtraClassValidation ()
{}
public static ValidationResult Validate( MyExtraClass myExtraClass )
{
if ( /**class is not valid*/)
return new ValidationResult( "Oops" );
return ValidationResult.Success;
}
}
Of course, your interfaces may be ablolutely anorther, but I reccomend you use it.
Also, you can call validateHandler from your control and check, for example, after every user key pressing.

Resources