Intersoft ClientUI 8 > ClientUI Controls > Control Library > Advanced Input Controls Overview > UXRangeSliderBar > How-to: Enable Data Validation in UXRangeSliderBar |
This example shows how to enable data validation in UXRangeSliderBar.
As an input control, you can enable data validation in UXRangeSliderBar. When the value of SelectionStart and SelectionEnd properties are bound to a data entity, you can throw an exception when the data is invalid.
The following code example shows how to validate the value of start and end price range using MVVM pattern.
Model |
Copy Code
|
---|---|
using ClientUI.Samples.ViewModels; namespace ClientUI.Samples.Models { public class PropertyFinder : ModelBase { public PropertyFinder() { } public bool EnableValidation { get; set; } private double _startPrice; public double StartPrice { get { return _startPrice; } set { if (_startPrice != value) { _startPrice = value; ClearError("StartPrice"); if (EnableValidation && (double.IsNaN(this.StartPrice) || this.StartPrice < 200.0)) SetError("StartPrice", "Price can not be smaller than $200,000."); OnPropertyChanged("StartPrice"); } } } private double _endPrice; public double EndPrice { get { return _endPrice; } set { if (_endPrice != value) { _endPrice = value; ClearError("EndPrice"); if (EnableValidation && (double.IsNaN(this.EndPrice) || this.EndPrice > 600.0)) SetError("EndPrice", "Price can not be larger than $600,000."); OnPropertyChanged("EndPrice"); } } } } } |
View Model |
Copy Code
|
---|---|
using ClientUI.Samples.Models; namespace ClientUI.Samples.ViewModels { public class PropertyFinderViewModel: ValidationViewModelBase { private PropertyFinder _propertyFinder; // Constructor public PropertyFinderViewModel() { this.PropertyFinder = new PropertyFinder { StartPrice = 300, EndPrice = 500 }; this.PropertyFinder.EnableValidation = true; } // Views public PropertyFinder PropertyFinder { get { return this._propertyFinder; } set { if (this._propertyFinder != value) { this._propertyFinder = value; OnPropertyChanged("PropertyFinder"); } } } } } |
View |
Copy Code
|
---|---|
<UserControl x:Class="ClientUI.Samples.Views.PropertyFinder" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:Intersoft="http://intersoft.clientui.com/schemas" xmlns:ViewModels="clr-namespace:ClientUI.Samples.ViewModels" mc:Ignorable="d" d:DesignHeight="300" d:DesignWidth="400"> <UserControl.Resources> <ViewModels:PropertyFinderViewModel x:Key="PropertyFinderViewModel"></ViewModels:PropertyFinderViewModel> </UserControl.Resources> <Grid x:Name="LayoutRoot" Background="White" DataContext="{StaticResource PropertyFinderViewModel}"> <Intersoft:FieldLabel Header="Select Price (in 1000 USD.): " HeaderWidth="170" VerticalAlignment="Top"> <Intersoft:UXRangeSliderBar HandlesVisibility="Visible" TickPlacement="BottomRight" TickFormat="C0" Minimum="100" Maximum="700" LargeChange="100" SmallChange="10" SelectionStart="{Binding PropertyFinder.StartPrice, Mode=TwoWay, ValidatesOnDataErrors=True, ValidatesOnExceptions=True}" SelectionEnd="{Binding PropertyFinder.EndPrice, Mode=TwoWay, ValidatesOnDataErrors=True, ValidatesOnExceptions=True}"/> </Intersoft:FieldLabel> </Grid> </UserControl> |
The following figure shows that the error message is displayed when the price range is smaller than 200,000 USD and larger than 600,000 USD.