0

I wanted to add validation rules to textBox ,the problem is that when I do the following code I got the following error message,

A value of type 'myValidations' cannot be added to a collection or dictionary of type 'Collection`1'.

The class myValidations roles is active.

What can be the problem?

    <TextBox x:Name="Name" Grid.Column="4" Margin="0,50,0,0"  Grid.Row="2" Style="{StaticResource tooltipError}">
        <Binding ElementName="textBlock" Path="Text">
                <Binding.ValidationRules>
                    <viewModel:MyValidationsRules/>
                </Binding.ValidationRules>
            </Binding>
    </TextBox>

The text boxes are inherit from the following style:

        <Style TargetType="TextBox">
            <Setter Property="AcceptsReturn" Value="True"/>
            <Setter Property="AllowDrop" Value="True"/>
            <Setter Property="HorizontalAlignment" Value="Left"/>
            <Setter Property="Height" Value="44"/>
            <Setter Property="Width" Value="199"/>
            <Setter Property="TextWrapping" Value="Wrap"/>
            <Setter Property="VerticalAlignment" Value="Top"/>
            <EventSetter Event="PreviewDragEnter"
                   Handler="DropText_PreviewDragEnter"/>
            <EventSetter Event="PreviewDrop"
                   Handler="DropText_PreviewDrop"/>
            <EventSetter Event="PreviewDragOver"

        </Style>
John Jerrby
  • 1,683
  • 7
  • 31
  • 68

2 Answers2

1

Not sure about your problem, but I think you have missed the <TextBox.Text> tag in your code.

<TextBox x:Name="Name" Grid.Column="4" Margin="0,50,0,0"  Grid.Row="2" Style="{StaticResource tooltipError}">
<TextBox.Text>
        <Binding ElementName="textBlock" Path="Text">
                <Binding.ValidationRules>
                    <viewModel:MyValidationsRules ValidatesOnTargetUpdated="True"/>
                </Binding.ValidationRules>
            </Binding>
</TextBox.Text>
    </TextBox>
Yongquan
  • 58
  • 5
  • Finally figured it out, the example from MSDN is wrong.... http://msdn.microsoft.com/en-us/library/ms753962%28v=vs.110%29.aspx You have to set ValidatesOnTargetUpdated="True" in the binding – Yongquan Jan 28 '14 at 15:49
  • HI Yongquan,I have tried it without success.any other idea? btw voted up for your trying to help :) – John Jerrby Jan 28 '14 at 16:14
  • I have tried run it and it run (the error is not disabled...) and when I type on the field I get the following error:{Cannot evaluate expression because the current thread is in a stack overflow state. While debug I saw that the validate method called multiple times...any idea? – John Jerrby Jan 28 '14 at 16:22
  • After More Than 3 hours trying to figure out what is the problem(doing clean & rebuild) The error was gone when I close and open the VS :( . Currently I have two issues Now the debugger is stopped in the validate method and when I return false the field was not change the color to red,why? 2. when I write long string and delete it with the backspace I get stack overflow exception ,how can i avoid it? Thanks again, Jhon – John Jerrby Jan 28 '14 at 16:55
  • Sorry, I've been away from desktop for a while, you can send some related codes to my email so I'll take a look. liuyongquan2010@gmail.com – Yongquan Jan 28 '14 at 17:17
0

I Think the problem is that your MyValidationsRules doesn't inherit of ValidationRule or your class MyValidationsRules not override the Validate method. This class and this method must be plubics.

public class MyValidationsRules : ValidationRule
{

    public override ValidationResult Validate(object value, CultureInfo cultureInfo)
    {

    }
}
PakKkO
  • 154
  • 5