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 a开发者_C百科nd 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="/"
精彩评论