开发者

WPF Textbox Binding not Responding when ValidationRule Fired

开发者 https://www.devze.com 2023-01-25 04:16 出处:网络
I have a textbox that has a ValidationRule applied to it: <TextBox Style=\"{StaticResource StandardTextBox}\"

I have a textbox that has a ValidationRule applied to it:

 <TextBox Style="{StaticResource StandardTextBox}" 
                 Grid.Column="1" Grid.Row="4" 
                 IsReadOnly="{Binding SaveModeText}"
                 MaxLength="50">
            <TextBox.Text>
                <Binding Path="Individual.SurName"
                         UpdateSourceTrigger="PropertyChanged"
                         ValidatesOnDataErrors="True"
                         ValidatesOnExceptions="True"
                         NotifyOnValidationError="True">
                    <Binding.ValidationRules>
                        <valid:RequiredTextBoxValidationRule 
                            ErrorMessage="Please enter a last name" />
                    </Binding.ValidationRules>
                </Binding>
            </TextBox.Text>
        </TextBox>

When the page开发者_如何学编程 loads the textbox contains the correct value based on its binding. If I delete the value from the textbox the ValidationRule fires properly and I see the error message as expected. My application contains a "Discard Changes" button which reloads the DataContext. The hope was it would reset all of the bindings and once again this textbox would display the original value. For some reason, all other values on the page that do not have a ValidationRule associated with them get reset properly, but this textbox does not.

If I remove the ValidationRule from the XAML the value resets properly. If I handle validation through IDataErrorInfo, the validation fires properly and the value resets properly. Because I have used ValidationRules throughout my application, I was wondering if anyone had come across this issue and resolved it. At this point I would prefer to stick with the implemented ValidationRules if possible, instead of switching everything over to IDataErrorInfo.


Since your you are modifying the value in code, wouldn't you need to have Mode=TwoWay in your binding for it to update? I don't have time to dig in and see if that's what's wrong, but it may be a place to start.

0

精彩评论

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