question

BrandonBoone-3625 avatar image
0 Votes"
BrandonBoone-3625 asked DaisyTian-1203 commented

WPF problem with using decimal points with ValidationRule and Lost Focus

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-wpf
· 1
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

@BrandonBoone-3625
Did my below answer work for you? Please let me know if there is anything that I can help here.

0 Votes 0 ·

1 Answer

DaisyTian-1203 avatar image
0 Votes"
DaisyTian-1203 answered DaisyTian-1203 edited

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.



2.gif (29.0 KiB)
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.