开发者

Validating data entered on the form page : WPF

I have around 20 controls which are binded to different properties of a class along with validation rules like following. For sake of understanding i am writing the code for one control as other are same.

<TextBox  Style="{StaticResource errorStyle}" Grid.Column="0" Grid.Row="2" Grid.RowSpan="1" HorizontalAlignment="Left" Margin="110,100,0,0" Name="balesText" VerticalAlignment="Top" Width="170" >
        <TextBox.Text>
            <Binding Source="{StaticResource insertTransaction}" UpdateSourceTrigger="Explicit" Path="Bales">
                <Binding.Valid开发者_开发知识库ationRules>
                    <ExceptionValidationRule/>
                </Binding.ValidationRules>
            </Binding>
        </TextBox.Text>
    </TextBox>

Right now i am checking for the input errors in code-behind in the following manner

BindingExpression balesBe = balesText.GetBindingExpression(TextBox.TextProperty);
balesBe.UpdateSource();
.
.
if (balesBe.HasError)
        {
            MessageBox.Show("Please correct Errors", "Insert Aborted");
        }
        else
        {
            Binding insertTransactionBinding = BindingOperations.GetBinding(balesText, TextBox.TextProperty);
            InsertTransaction insertTransaction = insertTransactionBinding.Source as InsertTransaction;
            insertMessage = insertTransaction.Add();
            MessageBox.Show(insertMessage, "Transaction");
            this.NavigationService.Refresh();
        }

Now, the Question is : Is there any way i can validate these 20 controls in one go or i need to manually define their BindingExpression and check for validation error??


Make your own UserControl "OwnTextBox". Inherit it from TextBox (OwnTextBox: TextBox) and define there DependencyProperty Validate. Then you only have to remember, that TextBox is part of logical tree and make search from it.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜