简体   繁体   中英

Validation rule not being called

I have the following code in my XAML:

<ItemsControl ItemsSource="{Binding Dimensions}">
            <ItemsControl.ItemTemplate>
                <DataTemplate>
                    <Grid>
                        <Grid.ColumnDefinitions>
                            <ColumnDefinition Width="Auto" />
                            <ColumnDefinition MinWidth="100" MaxWidth="300" />
                        </Grid.ColumnDefinitions>

                        <Label Grid.Column="0"
                               Content="Dimension x" 
                               Target="{Binding ElementName=DimTextBox}" />
                        <TextBox Grid.Column="1" Name="DimTextBox" >
                            <Binding Path="/"  UpdateSourceTrigger="PropertyChanged">
                                <Binding.ValidationRules>
                                    <valid:DataSetDimensionValidationRule />
                                </Binding.ValidationRules>
                            </Binding>
                        </TextBox>
                    </Grid>
                </DataTemplate>              
            </ItemsControl.ItemTemplate>                
        </ItemsControl>

Where Dimensions is an Observable collection of strings. It seems to bind ok, I get the expected number of labels and textboxes and the textboxes contain the default value. However, when I change something in the textbox, my validation rule doesn't get called.

I know it is probably something simple but I am stuck. Help?

Try this...

<TextBox Grid.Column="1" Name="DimTextBox" >
    <Binding ValidatesOnExceptions="True" Path="/" UpdateSourceTrigger="PropertyChanged">
        <Binding.ValidationRules>
            <valid:DataSetDimensionValidationRule />
        </Binding.ValidationRules>
    </Binding>
</TextBox>

Edit: If the above doesn't work, try messing around with these properties on the validation rule: http://msdn.microsoft.com/en-us/library/cc647541.aspx

I think you just need to set ValidatesOnDataErrors="True" on your binding element so it would look like this.

<TextBox Grid.Column="1" Name="DimTextBox" >
     <Binding Path="/"  UpdateSourceTrigger="PropertyChanged">
           <Binding.ValidationRules>
               <valid:DataSetDimensionValidationRule />
           </Binding.ValidationRules>
     </Binding>
</TextBox>

I'm not sure what the problem actually was but when I updated my Dimensions to be an ObservableCollection of DimensionView where DimensionView is my own class containing a Label and a Value it worked. Code:

<Label Grid.Column="0"
                           Content="{Binding Label}" 
                           Target="{Binding ElementName=DimTextBox}" />
                    <TextBox Grid.Column="1" Name="DimTextBox" >
                        <Binding Path="Value"  UpdateSourceTrigger="PropertyChanged">
                            <Binding.ValidationRules>
                                <valid:DataSetDimensionValidationRule />
                            </Binding.ValidationRules>
                        </Binding>
                    </TextBox>

I guess maybe it just didn't like Path="/"

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM