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

本文关键字:文本 设置 参数 方法 控件 WPF 调用 用户 | 更新日期: 2023-09-27 18:04:52

有一个主要的c#类CtrlMain和方法testForm,它打开一个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>

和c#代码是

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

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

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

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