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

mauede

Well-known member
Joined
Sep 1, 2021
Messages
103
Location
Northwood - UK
Programming Experience
Beginner
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;

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




    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;
                       InitializeComponent();
            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"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:LuiIuApp"
        mc:Ignorable="d"
        Title="MainWindow" Height="450" Width="800">
    <Window.Resources>
        <local:DoubleRangeRule x:Key="DoubleRangeRule"/>
        <Style x:Key="textBoxInError" TargetType="{x:Type TextBox}">
            <Style.Triggers>
                <Trigger Property="Validation.HasError" Value="true">
                    <Setter Property="ToolTip"  Value="{Binding RelativeSource={x:Static RelativeSource.Self},  Path=(Validation.Errors)/ErrorContent}"/>
                </Trigger>
            </Style.Triggers>
        </Style>
        <ControlTemplate x:Key="validationTemplate">
            <DockPanel>
                <TextBlock Foreground="Red" FontSize="20">!</TextBlock>
                <AdornedElementPlaceholder/>
            </DockPanel>
        </ControlTemplate>
    </Window.Resources>

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