如何使用WPF用户控件文本框中设置的参数在c#类中调用方法

如何使用WPF用户控件文本框中设置的参数在c#类中调用方法,c#,wpf,xaml,binding,C#,Wpf,Xaml,Binding,使用带有方法testForm的主c#classCtrlMain打开一个WPF表单(testFormCtrl),该表单显示一个文本框,并使用文本框中引入的值分配一个变量Xmin 我想从打开的wpf用户控件中执行方法wantToExe,并将文本框中引入的值作为参数 以下是我所拥有的: public partial class CtrlMain : UserControl { int mCounter; double firstPos; double[] currentB

使用带有方法
testForm
的主c#class
CtrlMain
打开一个WPF表单(
testFormCtrl
),该表单显示一个文本框,并使用文本框中引入的值分配一个变量
Xmin

我想从打开的wpf用户控件中执行方法
wantToExe
,并将文本框中引入的值作为参数

以下是我所拥有的:

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;

        }



}
testFormCtrl xaml是:

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

我如何从其他类调用该方法,我不能使其静态….

只需为您的
testFormCtrl
类创建一个新构造函数,该类接受
CtrlMain
作为参数:

private CtrlMain _caller;

public testFormCtrl(CtrlMain caller)
    : this()
{
    _caller = caller;
}
然后您可以调用它的方法:

private void ButtonExecuteX_Click(object sender, RoutedEventArgs e)
{
    if(_caller != null) caller.wantToExe(Xmin);
}
记住在
testForm
方法中传递
CtrlMain
的实例:

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

另一个问题是Xmin为0,并且没有在文本框中输入值,您知道原因是什么吗?这是另一个问题,无论如何,请尝试设置控件的DataContext。更多信息
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();
    }