0

Having a main c# class CtrlMain with method testForm it opens a WPF form (testFormCtrl)that shows a textbox and assigns a variable Xmin with the value introduced on textbox.

I want to execute method wantToExe from opened wpf user control with value introduced on textbox as a parameter

Here is what I have:

public partial class CtrlMain : UserControl
{
    int    mCounter;
    double firstPos;
    double[] currentBounds;
    //ETC..

    //constructor and class methods


    //this opens a user control
        static void testForm()
        {
            GenericWindow goWin;
            testFormCtrl mytestFormCtrl = new testFormCtrl();
            goWin = new GenericWindow(App.Current.MainWindow, mytestFormCtrl);
            goWin.Title = "test";
            goWin.ShowDialog();
        }


        //how to call this method with parameter of textbox?
        public  double wantToExe(double externalX){

            double result;

             //DO SOME COMPUTING 
            return result;

        }



}

the testFormCtrl xaml is:

<UserControl x:Class="testFormCtrl"
             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:telerik="http://schemas.telerik.com/2008/xaml/presentation"
             mc:Ignorable="d">
    <Grid Height="300">
            <Grid>
                <GroupBox Header="Location" Height="93" HorizontalAlignment="Left" Margin="4,3,0,0" Name="GBoxGridDefinition" VerticalAlignment="Top" Width="624">
                    <Grid>
                        <TextBlock Height="20" HorizontalAlignment="Left" Margin="20,13,0,0" Name="TblockXmin" Text="Xmin:" VerticalAlignment="Top" Width="36" />
                        <TextBox Name="TextBoxXmin" Height="20" Width="89"   HorizontalAlignment="Left" VerticalAlignment="Top" Margin="59,9,0,0" Text="{Binding Path=Xmin, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged, ValidatesOnDataErrors=True,NotifyOnValidationError=True}" >
                        </TextBox>
                        <telerik:RadButton Content="Execute X" IsEnabled="True" Height="22" HorizontalAlignment="Left" Margin="484,9,0,0" Name="ButtonExecuteX" VerticalAlignment="Top" Width="102" telerik:StyleManager.Theme="Vista" />                        
                    </Grid>
                </GroupBox>
            </Grid>
    </Grid>
</UserControl>

and c# code is

public partial class testFormCtrl : UserControl
    {
        double gnXmin;
        public event PropertyChangedEventHandler PropertyChanged;

        public double Xmin
        {
            get { return gnXmin; }
            set
            {
                gnXmin = value;
                OnPropertyChanged("Xmin");
            }
        }

        void OnPropertyChanged(string lcProperty)
        {
            if (this.PropertyChanged != null)
            {
                PropertyChanged(this, new PropertyChangedEventArgs(lcProperty));
            }
        }  

        public testFormCtrl ()
        {
            InitializeComponent();
        }



        private void ButtonExecuteX_Click(object sender, RoutedEventArgs e)
        {
            //how to call CtrlMain.wantToExe(Xmin) ???
        }
    }
}

How can I call that method from other class, I can not make it static....

1 Answer 1

1

Just create a new constructor for your testFormCtrl class that accepts CtrlMain as a parameter:

private CtrlMain _caller;

public testFormCtrl(CtrlMain caller)
    : this()
{
    _caller = caller;
}

Then you can just invoke its methods:

private void ButtonExecuteX_Click(object sender, RoutedEventArgs e)
{
    if(_caller != null) caller.wantToExe(Xmin);
}

Remember to pass the instance of CtrlMain in your testForm method:

static void testForm()
    {
        GenericWindow goWin;
        testFormCtrl mytestFormCtrl = new testFormCtrl(this); //use the new constructor
        goWin = new GenericWindow(App.Current.MainWindow, mytestFormCtrl);
        goWin.Title = "test";
        goWin.ShowDialog();
    }
Sign up to request clarification or add additional context in comments.

2 Comments

Other question is Xmin is 0, and is not getting value entered on text box, do you know what would be the reason?
That's another question, anyway try to set the DataContext of your control. More info here

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.