WPF problem with using decimal points with ValidationRule and Lost Focus

Brandon Boone 21 Reputation points
2021-04-13T19:01:12.073+00:00

here is my Code

 <StackPanel  Grid.Row="2" Grid.Column="0" Orientation="Horizontal" Grid.ColumnSpan="3" Height="auto" Grid.RowSpan="2"  >
                    <TextBox  Name="TextBox"  Width="50"   Margin="0,0,10,0" Height="21"  FontSize="12" ToolTip="{Binding RelativeSource={RelativeSource Self}, Path=(Validation.Errors)[0].ErrorContent}" >
                        <TextBox.Text>
                            <Binding Path="SetTemperature" UpdateSourceTrigger="PropertyChanged">
                                <Binding.ValidationRules>
                                    <local:TemperaturesValidationRule />
                                </Binding.ValidationRules>
                            </Binding>
                        </TextBox.Text>
                    </TextBox>

The problem I have is, if I enter an incorrect value my button becomes disable , which is right, However then when I try and change the value, It never clears the error on the textbox, because the button can't lose focus. it cant because the button is disable. So It can never clear the error.

If I use

UpdateSourceTrigger="PropertyChanged"
Then my decimal points will not show up and I do not know why.

and if I add a delay the decimals only show up part of the time.

Does anyone have a good working fixed for this? I cant be the only one with this problem.

c#

Windows Presentation Foundation
Windows Presentation Foundation
A part of the .NET Framework that provides a unified programming model for building line-of-business desktop applications on Windows.
2,853 questions
{count} votes

1 answer

Sort by: Most helpful
  1. DaisyTian-1203 11,641 Reputation points
    2021-04-14T02:49:20.707+00:00

    I create an int ValidationRule demo due to having no relevant information about decimal points with ValidationRule, below is my sample code:
    Xaml code:

     <Window.DataContext>  
            <local:ViewModel></local:ViewModel>  
        </Window.DataContext>  
    <Window.Resources>  
        <local:MyConverter x:Key="MyConver"></local:MyConverter>  
    </Window.Resources>  
        <Grid>  
            <TextBox  Name="TextBox1"  Width="50"   Margin="0,0,10,0" Height="21"  FontSize="12" ToolTip="{Binding RelativeSource={RelativeSource Self}, Path=(Validation.Errors)[0].ErrorContent}" >  
                <TextBox.Text>  
                    <Binding Path="SetTemperature" UpdateSourceTrigger="PropertyChanged" >  
                        <Binding.ValidationRules>  
                            <local:AgeRangeRule Min="21" Max="130"/>  
                        </Binding.ValidationRules>  
                    </Binding>  
                </TextBox.Text>  
            </TextBox>  
            <Button Content="Button" Width="120" Height="50" VerticalAlignment="Top" IsEnabled="{Binding ElementName=TextBox1,Path=(Validation.HasError),Converter={StaticResource MyConver}}"></Button>  
        </Grid>  
    

    C# code:

     public class AgeRangeRule : ValidationRule  
        {  
            public int Min { get; set; }  
            public int Max { get; set; }  
      
            public AgeRangeRule()  
            {  
            }  
      
            public override ValidationResult Validate(object value, CultureInfo cultureInfo)  
            {  
                int age = 0;  
      
                try  
                {  
                    if (((string)value).Length > 0)  
                        age = Int32.Parse((String)value);  
                }  
                catch (Exception e)  
                {  
                    return new ValidationResult(false, $"Illegal characters or {e.Message}");  
                }  
      
                if ((age < Min) || (age > Max))  
                {  
                    return new ValidationResult(false,  
                      $"Please enter an age in the range: {Min}-{Max}.");  
                }  
                return ValidationResult.ValidResult;  
            }  
        }  
    public class ViewModel  
        {  
            public int SetTemperature { get; set; }  
            public ViewModel()  
            {  
                SetTemperature = 50;  
            }  
        }  
      
        public class MyConverter : IValueConverter  
        {  
            public object Convert(object value, Type targetType, object parameter, CultureInfo culture)  
            {  
                bool flag = (bool)value;  
                flag = !flag;  
                return flag;  
            }  
      
            public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)  
            {  
                throw new NotImplementedException();  
            }  
        }  
    

    The result picture is:
    87459-2.gif

    If my answer didn't give you help, please show your rest of code which related to Button for me to make a demo to reproduce your error.


    If the response is helpful, please click "Accept Answer" and upvote it.
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.