Why is the C# variable not updated?

Edelweiss 1 Reputation point
2022-04-24T19:52:05.837+00:00

@Hui Liu-MSFT
Thank you for all your support. I still need a little bit of help.
With reference to your answer-3 that draws a red vertical arrow beside the TextBox when a number is entered that is outside the allowed range.
I integrated your code into my application. It works.
However, the C# variable DiceThreshold bound to the TextBox Text is not being updated. I need to have whatever value is entered in the Text field copied into the C# variable DiceThreshold as well.
I modified your XAML code by adding "Mode=TwoWay", and also tried adding "ElemenName" (see attached image)

In both cases, the data-validation procedure is no more engaged regardless of the number entered in the text field. Furthermore, the entered value is not copied back into the C# variable DiceThreshold.
Could you please help me with validating the entered number but also getting it copied to the variable DiceThreshold?

Thank you in advance.

195921-mode-twoway.png

XAML
XAML
A language based on Extensible Markup Language (XML) that enables developers to specify a hierarchy of objects with a set of properties and logic.
766 questions
0 comments No comments
{count} votes

3 answers

Sort by: Most helpful
  1. Hui Liu-MSFT 40,266 Reputation points Microsoft Vendor
    2022-04-25T05:23:37.923+00:00

    You could make the property's class implement INotifyPropertyChanged to update property notifications.

    MainWindow.xaml.cs:

    using System;  
    using System.ComponentModel;  
    using System.Globalization;  
    using System.Runtime.CompilerServices;  
    using System.Windows;  
    using System.Windows.Controls;  
      
    namespace TextChangedValidation  
    {  
      public partial class MainWindow : Window, INotifyPropertyChanged  
      {  
        private double diceThreshold= 0.3;  
        public double DiceThreshold   
        {  
          get { return diceThreshold;}  
      
          set { diceThreshold=value; OnPropertyChanged("DiceThreshold");}  
        }  
        public MainWindow()  
        {  
          InitializeComponent();  
          DataContext = this;  
        }  
      
        public event PropertyChangedEventHandler PropertyChanged;  
        protected void OnPropertyChanged([CallerMemberName] string name = null)  
        {  
          PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(name));  
        }  
      }  
      public class DoubleRangeRule : ValidationRule  
      {  
        public double Min { get; set; }  
      
        public double Max { get; set; }  
      
        public override ValidationResult Validate(object value, CultureInfo cultureInfo)  
        {  
          double parameter = 0;  
      
          try  
          {  
            if (((string)value).Length > 0)  
            {  
              parameter = Double.Parse((String)value);  
            }  
          }  
          catch (Exception e)  
          {  
            return new ValidationResult(false, "Illegal characters or " + e.Message);  
          }  
      
          if ((parameter < this.Min) || (parameter > this.Max))  
          {  
            return new ValidationResult(false,  
                "Please enter value in the range: "  
                + this.Min + " - " + this.Max + ".");  
          }  
          return new ValidationResult(true, null);  
        }  
      }  
    }  
    

    MainWindow.xaml:

    196690-b.txt
    The result:
    195956-44.gif
    196771-image.png


    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. mauede 221 Reputation points
    2022-04-29T00:00:09.15+00:00

    @Hui Liu-MSFT
    I have uploaded "MainWindow.XAML.cs", "MainWindow.XAML", "PTAccess.cs". I had to add the extension ".txt" to be able to upload them. Please, remove the ".txt" and you'll have the original files.
    I have integrated your code in "MainWindow.XAML.cs" and "MainWindow.XAML".
    The value entered in "DiceTol.Text" is validated. The message pops up when the value exceeds 1 and a red arrow is visible beside the TextBox (see GUI). However, the property "DiceThreshold" contains 0.33
    regardless of what appears inside DiceTol.Text in the GUI.
    I need the property DiceThreshold to contain exactly the value displayed in the GUI. In fact, function "AutoMatchedStruct_Click", defined in MainWindow.XAML.cs, reset DIceThreshold to its default value (0.33) and it passes it to function "StructuresAutomaticRename" that is defined in file "PTAccess.cs". It is used by the function "StructuresAutomaticRename"
    to select the element in the rightmost column to be saved to the Velocity database.

    The problem is that the value entered in DiceTol.text is not propagated back to property Dicethreshold. Or if it is then DiceThreshold content is overwritten as it always contains 0.33 even if the value entered is, for example, 0.5 (acceptable).

    Thank you for all your help

    Th197561-gui.png

    197527-mainwindowxamlcs.txt197544-mainwindowxaml.txt197545-ptaccesscs.txt


  3. Edelweiss 1 Reputation point
    2022-04-29T09:45:24.65+00:00

    @Hui Liu-MSFT
    Hi. Could you download the code I upload last night?
    You should be able to download and inspect "MainWindow.XAML", "MainWindow.XAML.cs" , "PTAccess.cs".
    I had to add the extension ".txt" to upload them.
    So you should remove the ".txt" after downloading.

    By placing a breakpoint I found out that the following statement is never executed:

     set { diceThreshold=value; OnPropertyChanged("DiceThreshold");}  
    

    That explains why the property "DiceThreshold" is not updated with the value entered into TextBox "DiceTol.Text".
    Why is such a setter statement never executed?

    If you place a breakpoint in your code you will find out yourself that the setter statement is never executed.
    Thank you