473,387 Members | 1,766 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

WPF DependencyProperty Validation Binding fails

EARNEST
128 100+
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 1953

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

Similar topics

1
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
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
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
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
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
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
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
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
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
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
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
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
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

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.