Question How to get full property updated with the contains of a TextBox


Well-known member
Sep 1, 2021
Northwood - UK
Programming Experience
I tried to integrate an example I found on the internet with my application but it doesn't work.
Basically. my GUI contains a TextBox where the user can enter a value between 0 and 1.
My goal is to check that the entered value is in the range [0,1] and also to have the property DiceThreshold updated with whatever number is entered in the TextBox. I found an example that I adapted and integrated with my application. The result is that the value entered in the TextBox is validated but the property DiceThreshold is not updated with the entered value.
The code clip dealing with data validation and property updating is the following:

C# code:
using System;
using System.ComponentModel;
using System.Globalization;
using System.Runtime.CompilerServices;
using System.Windows;
using System.Windows.Controls;

namespace TestApp
    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;

                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);

    public partial class MainWindow : Window, INotifyPropertyChanged
        private double diceThreshold = 0.3;
        public double DiceThreshold
            get { return diceThreshold; }

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

        public MainWindow()
            DataContext = this;
            MessageBox.Show($"DiceThreshold:  {DiceThreshold}", "My App", MessageBoxButton.OK, MessageBoxImage.Information);


        public event PropertyChangedEventHandler PropertyChanged;
        protected void OnPropertyChanged([CallerMemberName] string name = null)
            PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(name));

XAML code:
<Window x:Class="LuiIuApp.MainWindow"
        Title="MainWindow" Height="450" Width="800">
        <local:DoubleRangeRule x:Key="DoubleRangeRule"/>
        <Style x:Key="textBoxInError" TargetType="{x:Type TextBox}">
                <Trigger Property="Validation.HasError" Value="true">
                    <Setter Property="ToolTip"  Value="{Binding RelativeSource={x:Static RelativeSource.Self},  Path=(Validation.Errors)/ErrorContent}"/>
        <ControlTemplate x:Key="validationTemplate">
                <TextBlock Foreground="Red" FontSize="20">!</TextBlock>

        <TextBox x:Name="DiceTol"    Background="Wheat" Width="100" Height="46"  Validation.ErrorTemplate="{StaticResource validationTemplate}"   Style="{StaticResource textBoxInError}">
                <Binding Path="DiceThreshold"   Mode="TwoWay"  StringFormat="N2"  UpdateSourceTrigger="PropertyChanged">
                        <local:DoubleRangeRule Min="0.0" Max="1.0"/>
        </ Grid>

How can I fix the problem that DiceThresold is not updated with the value entered in the TextBox?
Thank you very much
Top Bottom