简体   繁体   中英

How to change field to original value using a "reset" button in wpf

I have a class that has several properties, one of which is editable, another of which is calculated based on the editable value. I want to initialize the editable value with something and allow the user to change it however they wish. However, I also want to have a reset button that puts the original value back into the textbox. I have a third variable that stores the value of the original number. However, I am not sure how I am supposed to access the object when the reset button is clicked to put the value back into the textbox. I've put the relevant code below (if I shouldn't be posting everything please let me know, still new to stackoverflow how-to):

Main Window

C#

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using HDR_BED_Calc_local.Testers;
using HDR_BED_Calc_local.Helpers;

namespace HDR_BED_Calc_local
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
            //var window = new MainWindow();
            this.DataContext = new fraction_doses(800, 700, 900, 800, 900, 1, 3);

        }
    }
}

XAML

<Window x:Class="HDR_BED_Calc_local.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:HDR_BED_Calc_local"
        xmlns:uctesters="clr-namespace:HDR_BED_Calc_local.Testers"
        mc:Ignorable="d"
        Title="MainWindow" Height="450" Width="800">
    <Grid>
        <uctesters:fx_tester x:Name="fxtester_UC"/>
    </Grid>
</Window>

Custom user control

C#

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using HDR_BED_Calc_local.Helpers;

namespace HDR_BED_Calc_local.Testers
{
    /// <summary>
    /// Interaction logic for fx_tester.xaml
    /// </summary>
    public partial class fx_tester : UserControl
    {
        public fx_tester()
        {
            InitializeComponent();
            
        }

        private void Button_Click(object sender, RoutedEventArgs e)
        {
            // What do I put here??
        }
    }
}

XAML

<UserControl x:Class="HDR_BED_Calc_local.Testers.fx_tester"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
             xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
             xmlns:local="clr-namespace:HDR_BED_Calc_local.Testers"
             mc:Ignorable="d" 
             d:DesignHeight="450" d:DesignWidth="800" Background="White">
    <StackPanel HorizontalAlignment="Center">
        <StackPanel Orientation="Horizontal">
            <TextBlock xml:space="preserve">Fraction: </TextBlock>
            <TextBlock Text="{Binding Path=fraction_number}"></TextBlock>
        </StackPanel>
        <StackPanel Orientation="Horizontal">
            <TextBlock xml:space="preserve">Current Dose: </TextBlock>
            <TextBox x:Name="curr_fx_tb" Text="{Binding Path=editable_fraction_dose, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"></TextBox>
        </StackPanel>
        <StackPanel Orientation="Horizontal">
            <TextBlock xml:space="preserve">EQD2: </TextBlock>
            <TextBlock Text="{Binding Path=this_fx_brachy_EQD2, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"/>
        </StackPanel>
        <Button Content="Reset" Click="Button_Click"/>
    </StackPanel>
</UserControl>

Helper function with class

C#

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.ComponentModel;
using System.Runtime.CompilerServices;

namespace HDR_BED_Calc_local.Helpers
{
    internal class fraction_doses : INotifyPropertyChanged
    {
        public int fraction_number { get; set; }
        public double alpha_beta { get; }
        private double _editable_fraction_dose;
        public double editable_fraction_dose
        {
            get { return _editable_fraction_dose; }
            set
            {
                _editable_fraction_dose = value;
                calc_EQD2();
                this.OnPropertyChanged("editable_fraction_dose");
            }
        }
        public double actual_fraction_dose { get; }
        public double pear_plan_dose { get; }
        public double IMRT_plan_dose { get; }
        public double max_dose_limit { get; }
        public double preferred_dose_limit { get; }

        private double _this_fx_brachy_EQD2;
        public double this_fx_brachy_EQD2 
        { 
            get { return this._this_fx_brachy_EQD2; }
            set
            {
                this._this_fx_brachy_EQD2 = value;
                this.OnPropertyChanged("this_fx_brachy_EQD2");
            }
        
        }

        public event PropertyChangedEventHandler PropertyChanged;
        public fraction_doses(double current_dose, double pear_dose, double IMRT_dose, double max_dose_limit, double preferred_dose_limit, int fraction_number, int alpha_beta)
        {
            this.alpha_beta = alpha_beta;
            this.actual_fraction_dose = current_dose;
            this.editable_fraction_dose = current_dose;
            this.pear_plan_dose = pear_dose;
            this.IMRT_plan_dose = IMRT_dose;
            this.max_dose_limit = max_dose_limit;
            this.preferred_dose_limit = preferred_dose_limit;
            this.fraction_number = fraction_number;
        }

        public void calc_EQD2()
        {
            // EQD2 is always reported in Gray even though we will be reporting cGy
            double dose_Gy = this.editable_fraction_dose/100;
            
            this.this_fx_brachy_EQD2 = Math.Round(1 * dose_Gy * (1+ dose_Gy / alpha_beta)/(1+2/alpha_beta), 2);
        }

        public void OnPropertyChanged([CallerMemberName] string propertyName = null)
        {
            if (PropertyChanged != null)
            {
                this.PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
            }
            PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
        }
    }
}

You can try the following:

private void Button_Click(object sender, RoutedEventArgs e)
{
    // What do I put here??
    var dc = (fraction_doses ) this.DataContext:
    if(dc!=null)
    dc.Clear();
}

in your fraction_doses class add the method that clears the properties

public void Clear(){
    this.MyProperty = MY_DEFAULT_VALUE;
    //clear other properties..
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM