1

I am really struggling here being a newbie programmer... This has all been done in WPF by the way

I have three textboxes in my MainWindow.

The method which returns the values to these textboxes has been made public (and is within MainWindow). Below is one of the three textboxes.

 public float GetLEL()
    {
        bool LEL = false;
        float parsedLELValue = 0;
        LEL = float.TryParse(LEL_TextBox.Text, out parsedLELValue);
        return parsedLELValue;
    }

How can I return this value into my usercontrol class as is, even if it changes?

I have tried all sorts such as creating an instance within the Usercontrol (which haven't worked) -

Application app = new Application();


private float GetNewLEL()
        {
            float parsedNewLELValue = 0.00F;
            bool NewLEL = false;
            if (HolidayPay_CheckBox.IsChecked == false)
            {
                NewLEL = float.TryParse(app.GetHPR().ToString, out parsedNewLELValue);
            }

            else if (HolidayPay_CheckBox.IsChecked == true)
            {
                parsedNewLELValue = 0.00F;
            }

            return parsedNewLELValue;

        }

However, the instance in the Usercontrol is not finding the GetLEL() Method from within the MainWindow. Can someone please help. Someone else has suggested Get and Set but I am not sure of how to do this.

4

1 回答 1

0

我试图重现我理解的你想要实现的目标;如果您对此代码有任何疑问,我很乐意回答。

MainWindow.xaml :

<Window x:Class="WpfApplication16.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="350" Width="525">
    <Grid>
        <StackPanel>
            <CheckBox IsChecked="{Binding MySuperCheckBoxIsChecked}" />

            <TextBox Text="{Binding MySuperFloatValue}" />

            <Button Click="Button_Click">Click me !</Button>

            <TextBlock x:Name="MySuperTextBlock"/>
        </StackPanel>
    </Grid>
</Window>

MainWindows.xaml.cs :

using System.Windows;

namespace WpfApplication16
{
    public partial class MainWindow : Window
    {
        private MySuperDataContextClass _mySuperDataContextClass = 
                                           new MySuperDataContextClass();

        public MainWindow()
        {
            InitializeComponent();

            this.DataContext =_mySuperDataContextClass;
        }

        private void Button_Click(object sender, RoutedEventArgs e)
        {
            if (_mySuperDataContextClass.MySuperCheckBoxIsChecked)
            {
                MySuperTextBlock.Text = 
                   _mySuperDataContextClass.MySuperFloatValue.ToString();
            }
            else
            {
                MySuperTextBlock.Text = 0.0f.ToString();
            }
        }
    }
}

MySuperDataContextClass.cs:

using System.ComponentModel;

namespace WpfApplication16
{
    public class MySuperDataContextClass : INotifyPropertyChanged
    {
        public event PropertyChangedEventHandler PropertyChanged;

        private bool _mySuperCheckBoxIsChecked;
        private float _mySuperFloatValue;

        public bool MySuperCheckBoxIsChecked
        {
            get { return _mySuperCheckBoxIsChecked; }

            set 
            {
                _mySuperCheckBoxIsChecked = value;

                if (this.PropertyChanged != null)
                {
                    this.PropertyChanged(this,
                     new PropertyChangedEventArgs("MySuperCheckBoxIsChecked"));
                }
            }
        }

        public float MySuperFloatValue
        {
            get { return _mySuperFloatValue; }

            set 
            { 
                _mySuperFloatValue = value;

                if (this.PropertyChanged != null)
                {
                    this.PropertyChanged(this,
                     new PropertyChangedEventArgs("MySuperFloatValue"));
                }
            }
        }
    }
}
于 2012-08-28T14:33:53.637 回答