473,486 Members | 2,114 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

WPF DependencyProperty Validation Binding fails

EARNEST
128 New Member
0 down vote favorite


I am trying to create a validation rule for a given control (in this scenario, it is the TextBox).

I am not able to obtain a successful Binding to the property of an object, although appropriate steps were taken: ValidationRule and DepedencyProperty are taken advantage of.

Kindly find code below. A side note is that "Is Required" in the custom Validation class is always False, unless I explicitly set the value in the XAML (no Binding, as per "Is Ranged" parameter).

Alternative is to use IDataErrorInfo interface, but I would like to stick to the ValidationRules.

Any tips and suggestions are appreciated.

Thank you in advance :)


XAML Code:

Expand|Select|Wrap|Line Numbers
  1. <TextBox Style="{StaticResource ValidationError}" LostFocus="ForceValidationCheck"
  2.          Visibility="{Binding Type, Converter={StaticResource Visibility}, ConverterParameter='Number'}"
  3.          IsEnabled="{Binding RelativeSource={x:Static RelativeSource.Self}, Converter={StaticResource IsEnabled}}">
  4.     <TextBox.Text>
  5.         <Binding Path="Value">
  6.             <Binding.ValidationRules>
  7.                 <validation:NumericValidation>
  8.                     <validation:NumericValidation.Dependency>
  9.                         <validation:NumericDependency IsRequired="{Binding Path=IsRequired}" IsRanged="True" Min="5"/>
  10.                     </validation:NumericValidation.Dependency>
  11.                 </validation:NumericValidation>
  12.             </Binding.ValidationRules>
  13.         </Binding>
  14.     </TextBox.Text>
  15. </TextBox>
  16.  
Validation Class:

Expand|Select|Wrap|Line Numbers
  1. public NumericDependency Dependency { get; set; }
  2.  
  3. public override ValidationResult Validate(object value, CultureInfo cultureInfo)
  4. {
  5.     isRequired = Dependency.IsRequired;
  6. }
  7.  
Validation Dependency Class:

Expand|Select|Wrap|Line Numbers
  1. public static readonly DependencyProperty IsRequiredProperty =
  2.         DependencyProperty.Register("IsRequired", typeof(bool), typeof(NumericDependency), new UIPropertyMetadata(default(bool)));
  3.  
  4. public bool IsRequired
  5. {
  6.     get
  7.     {
  8.         return (bool) GetValue(IsRequiredProperty);
  9.     }
  10.     set
  11.     {
  12.         SetValue(IsRequiredProperty, value);
  13.     }
  14. }
  15.  
Dec 6 '12 #1
0 1956

Sign in to post your reply or Sign up for a free account.

Similar topics

1
281
by: Marcin Floryan | last post by:
Hello! My question regards opening (and re-opening) Form and the Load event. I have a main form (frmMain) and I also have a data form (frmData). In the main form I have created: Private...
4
2608
by: bnp | last post by:
Hi All, I am quite new the JavaScript. Basically I am a C++ programmer, but now I am working on JavaScript since last 5 days. I have a problem regarding the form validation. I have created a...
2
1715
by: Danny | last post by:
I created a function to test to see if entered text has a '.' in it, The code seems to work but the validation always fails even when 'true' is returned. i put = myfunction in the validation...
5
3219
by: Chris | last post by:
Based upon some prevoius postings on what to do for adding a 'add' row to a datagrid I utilize the footer to create the 'add' row. The only issue is that I have it sharing the 'UpDate_Command' and...
7
10254
by: christian.eickhoff | last post by:
Hi Everyone, I am currently implementing an XercesDOMParser to parse an XML file and to validate this file against its XSD Schema file which are both located on my local HD drive. For this...
2
3677
by: kogrover | last post by:
ISSUE: COM Excel Sort works with Early Binding, but not Late Binding, but py2exe only does Late Binding I have code similar to this (type from notes, so there may be a typo...) import...
4
997
by: Morten Snedker | last post by:
http://test.planprojekt.dk/vaerk.aspx (http://validator.w3.org/check?uri=http%3A%2F%2Ftest.planprojekt.dk%2Fvaerk.aspx) fails validation. It fails due to tags not being closed properly. However,...
2
6722
by: John Smith | last post by:
Hello, I have a VB.NET application with a Windows form that have several textboxes fields where I have dates entered. I would like to do a date validation check after the the field is updated, so...
0
994
by: shekaranjanappa | last post by:
Hi I have a requirement to do follwing operation in looop 1. open socket 2. start 3. bind 4. listion 5. close socket Here used ip address and port is same always.
7
7974
by: Some Bloke | last post by:
I am trying to bind a WPF dependency property to an aggregate of a collection. I've implemented a wrapper around a Dictionary that implements INotifyCollectionChanged to fire the event when items...
0
6964
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7175
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
6842
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
7330
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
1
4865
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
3070
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
3070
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1378
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
598
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.