Pregunta

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>
¿Fue útil?

Solución

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>

Otros consejos

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)
    {

    }
}
Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top