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:
If the response is helpful, please click "Accept Answer" and upvote it.
Note: Please follow the steps in our [documentation][5] to enable e-mail notifications if you want to receive the related email notification for this thread.
[5]: https://learn.microsoft.com/en-us/answers/articles/67444/email-notifications.html