使用方法CtrlMain
创建一个主c#class testForm
,它会打开一个WPF表单(testFormCtrl
),该表单显示一个文本框,并为变量Xmin
指定一个值文本框。
我想从打开的wpf用户控件执行方法wantToExe
,并在textbox上引入值作为参数
这就是我所拥有的:
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) ???
}
}
}
如何从其他类调用该方法,我无法使其静态....
答案 0 :(得分:1)
只需为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);
}
请记住在CtrlMain
方法中传递testForm
的实例:
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();
}