简体   繁体   English

未调用验证规则

[英]Validation rule not being called

I have the following code in my XAML: 我的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. 其中Dimensions是可观察的字符串集合。 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 编辑:如果上述方法不起作用,请尝试验证规则上的以下属性: http : //msdn.microsoft.com/zh-cn/library/cc647541.aspx

I think you just need to set ValidatesOnDataErrors="True" on your binding element so it would look like this. 我认为您只需要在绑定元素上设置ValidatesOnDataErrors =“ True”即可,如下所示。

<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. 我不确定实际上是什么问题,但是当我将我的Dimensions更新为DimensionView的ObservableCollection时,其中DimensionView是我自己的包含Label和Value的类。 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="/" 我想也许只是不喜欢Path =“ /”

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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