question

mauede-6322 avatar image
0 Votes"
mauede-6322 asked mauede-6322 commented

Why is the C# variable not updated?

@HuiLiu-MSFT
Thank you for all your help.
The problem I am having with the code you sent me is that if a wrong value is entered in the TextBox (DiceTol.Text) a red arrow appears beside the entered value and, as you stated, the property DiceThreshold is not updated with the wrong value. The wrong value keeps being displayed in the TextBox because the code has no way to know whether the value entered is right or wrong.
When the button "Load Matched Structures" is clicked, the content of the property DiceThreshold is used to sift out the names displayed in the rightmost column. The code uses the Dice coefficient to automatically rename the strings in the 4th column according to the gold standard string in the leftmost column. The Dice Coefficient returns an estimate of the guess goodness between 0 and 1. By entering a value between 0 and 1 in the TextBox (DiceTol.Text) the user decides to discard all the guessed strings that scored a value below the entered threshold.
When the code has to decide which strings to display in the rightmost column, it needs to know the content of DiceThreshold. You are telling me DiceThreshold is updated if and only if the entered value is between 0 and 1 so I can use it safely (I will check that).
However, when the entered value is not accepted then it has to be reset to its default value when the button "Load Matched Structures" is clicked.
How can the code that handles the clicking event know when to use the content of DiceThreshol and when to use the default value?
If I define a boolean variable inside the namespace but outside the Mainwindow class then will your data validation procedure be able to set it to True when a right value is entered and to False when a wrong value is entered? Will that boolean variable be accessible by the code inside MainWindow class?
Thank you

dotnet-wpf-xaml
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.

1 Answer

HuiLiu-MSFT avatar image
0 Votes"
HuiLiu-MSFT answered mauede-6322 commented

MainWindow.xaml:

198502-c.txt
MainWindow.xaml.cs:

 using System;
 using System.Collections.Generic;
 using System.Collections.ObjectModel;
 using System.Linq;
 using System.Windows;
 using System.Windows.Controls;
 using System.Windows.Data;
 using System.Windows.Input;
 using System.ComponentModel;
 using System.Globalization;
 using System.Runtime.CompilerServices;
    
 namespace PropertyNotifyDemo
 {
   public partial class MainWindow : Window, INotifyPropertyChanged
   {
     private double diceThreshold = 0.33;
     public double DiceThreshold
     {
       get { return diceThreshold; }
    
       set { diceThreshold = value; OnPropertyChanged("DiceThreshold"); }
     }
     private bool _hasError = false;
    
     public bool HasError
     {
       get
       {
         return _hasError;
       }
    
       set
       {
         _hasError = value;
         OnPropertyChanged("HasError");
       }
     }
     const double DiceThresholdDefault = 0.33;
     public List<T> InitList<T>(int count, T initValue)
     {
       return Enumerable.Repeat(initValue, count).ToList();
     }
     public MainWindow()
     {
       InitializeComponent();
       DataContext = this;
       
     }
     public event PropertyChangedEventHandler PropertyChanged;
     protected void OnPropertyChanged([CallerMemberName] string name = null)
     {
       PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(name));
     }
    
     private void Button_Click(object sender, RoutedEventArgs e)
     {
       if (HasError == true)
       {
         DiceThreshold= DiceThresholdDefault;
       }
     }
   }
   public enum SpecialFeatures
   {
     None,
     Color,
     Highlight
   }
   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)
       {
         MessageBox.Show("Illegal characters or " + e.Message, "Warning ", MessageBoxButton.OK, MessageBoxImage.Warning);
         return new ValidationResult(false, "Illegal characters or " + e.Message);
       }
    
       if ((parameter < this.Min) || (parameter > this.Max))
       {
         MessageBox.Show("Invalid Dice Threshold. Enter a number between " + this.Min + " and " + this.Max, "Warning ", MessageBoxButton.OK, MessageBoxImage.Warning);
         return new ValidationResult(false,
             "Please enter value in the range: "
             + this.Min + " - " + this.Max + ".");
       }
       return new ValidationResult(true, null);
     }
   }
   public class PTAccess
   {
     static void OrThrow(bool result, dynamic source)
     {
       if (!result)
       {
         string errorMessage = source.getErrorMessage();
         MessageBox.Show($"{errorMessage}. Abort application! ", "Error ", MessageBoxButton.OK, MessageBoxImage.Error);
         App.Current.Shutdown();                             // TERMINATE APPLICATION
         Application.Current.MainWindow.Close();
       }
     }
   }
    
   public class ProtocolSettingsLayout
   {
     public static readonly DependencyProperty MVVMHasErrorProperty = DependencyProperty.RegisterAttached("MVVMHasError",
                                                                     typeof(bool),
                                                                     typeof(ProtocolSettingsLayout),
                                                                     new FrameworkPropertyMetadata(false,
                                                                                                   FrameworkPropertyMetadataOptions.BindsTwoWayByDefault,
                                                                                                   null,
                                                                                                   CoerceMVVMHasError));
    
     public static bool GetMVVMHasError(DependencyObject d)
     {
       return (bool)d.GetValue(MVVMHasErrorProperty);
     }
    
     public static void SetMVVMHasError(DependencyObject d, bool value)
     {
       d.SetValue(MVVMHasErrorProperty, value);
     }
    
     private static object CoerceMVVMHasError(DependencyObject d, Object baseValue)
     {
       bool ret = (bool)baseValue;
    
       if (BindingOperations.IsDataBound(d, MVVMHasErrorProperty))
       {
         if (GetHasErrorDescriptor(d) == null)
         {
           DependencyPropertyDescriptor desc = DependencyPropertyDescriptor.FromProperty(Validation.HasErrorProperty, d.GetType());
           desc.AddValueChanged(d, OnHasErrorChanged);
           SetHasErrorDescriptor(d, desc);
           ret = System.Windows.Controls.Validation.GetHasError(d);
         }
       }
       else
       {
         if (GetHasErrorDescriptor(d) != null)
         {
           DependencyPropertyDescriptor desc = GetHasErrorDescriptor(d);
           desc.RemoveValueChanged(d, OnHasErrorChanged);
           SetHasErrorDescriptor(d, null);
         }
       }
    
       return ret;
     }
    
     private static readonly DependencyProperty HasErrorDescriptorProperty = DependencyProperty.RegisterAttached("HasErrorDescriptor",
                                                                             typeof(DependencyPropertyDescriptor),
                                                                             typeof(ProtocolSettingsLayout));
    
     private static DependencyPropertyDescriptor GetHasErrorDescriptor(DependencyObject d)
     {
       var ret = d.GetValue(HasErrorDescriptorProperty);
       return ret as DependencyPropertyDescriptor;
     }
    
     private static void OnHasErrorChanged(object sender, EventArgs e)
     {
       DependencyObject d = sender as DependencyObject;
    
       if (d != null)
       {
         d.SetValue(MVVMHasErrorProperty, d.GetValue(Validation.HasErrorProperty));
       }
     }
    
     private static void SetHasErrorDescriptor(DependencyObject d, DependencyPropertyDescriptor value)
     {
       var ret = d.GetValue(HasErrorDescriptorProperty);
       d.SetValue(HasErrorDescriptorProperty, value);
     }
   }
    
 }

The result:

198482-6.gif


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. 


c.txt (3.5 KiB)
6.gif (174.2 KiB)
· 3
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.

@HuiLiu-MSFT
Thank you for all your commitment.
I Have to find some time to integrate your code into my application and understand why it has become so complicated.
I am working on manipulating DICOM files with MatLab right now. I have a very important presentation on May 16.
After that date, I will devote some time to improving my GUI with your code. Please, bear with me.
Thank you

0 Votes 0 ·

Hi,@mauede-6322. Is there any progress on the issue? Did my answer solve your problem? If so, you could accept it as the answer. It's helpful for community members with similar questions.

0 Votes 0 ·

@ HuiLiu-MSFT
Sorry for my delayed reply.
I have accepted your answer as I could use the previous code release you posted.
I have had no time to be able to integrate this new code release you posted with my script.
I am working on manipulating a huge amount of DICOM files.
It will take me some time to understand your latest code snippet as you have introduced some new functions I am not familiar with.

Questions:

  1. Is this latest code snippet drawing the red arrow beside the TextBox containing the wrong value?

  2. Does the red arrow disappear when the user rases the wrong value and enters the right value?

  3. Is the C# variable "DicomThreshold" updated with whatever value (right/wrong) the user has entered ?

Thank you



0 Votes 0 ·