将文件名(字符串参数)从子窗口传递到父窗口的非静态方法
我需要从父窗口打开一个文件并使用其内容填充不同的表格和控件(属于父窗口)。文件的名称(字符串)由 DataGrid.SelectedItem 在子窗口中形成
private void LoadResultsCommand_Executed(object sender, ExecutedRoutedEventArgs e)
{
var row = pastTestResultsDataGrid.SelectedItem as DataRowView;
if (row != null)
{
string fileName = row[0] + " " + row[1] + " " + row[2] + " " + row[3] + " " + row[4] + " " +
((DateTime)row[6]).ToShortDateString().Replace('/', '-') + " " + ((DateTime)row[7]).ToShortDateString().Replace('/', '-') + " .dat";
MainWindow.LoadResults(fileName);
}
}
正如您在父窗口(MainWindow)中看到的那样,我必须使用静态方法
public static void LoadResults(string fileName)
{
string fullFileName = @"C:\Users\Public\Documents\Test Data\" + fileName;
var binFormat = new BinaryFormatter();
var testData = new TestData();
if (File.Exists(fullFileName))
{
using (Stream fStream = new FileStream(fullFileName, FileMode.Open))
{
testData = (TestData) binFormat.Deserialize(fStream);
}
}
//here I am trying to load data from testData instance of TestData class into data
//tables or set Text property of a TextBox. Can't access them from a static method!
}
我知道我什至不应该尝试从静态方法访问非静态成员。我只是想解释我的任务。 WPF(其中类 Window 在 XAML 中定义)中是否有任何方法可以在运行时访问父窗口的实例及其方法?如果有更优雅和更简单的解决方案,我不介意改变总体方法。
From Parent window I need to open a file and fill up different tables and controls (belong to Parent window) using its content. The name of the file (string) is formed in Child window by DataGrid.SelectedItem
private void LoadResultsCommand_Executed(object sender, ExecutedRoutedEventArgs e)
{
var row = pastTestResultsDataGrid.SelectedItem as DataRowView;
if (row != null)
{
string fileName = row[0] + " " + row[1] + " " + row[2] + " " + row[3] + " " + row[4] + " " +
((DateTime)row[6]).ToShortDateString().Replace('/', '-') + " " + ((DateTime)row[7]).ToShortDateString().Replace('/', '-') + " .dat";
MainWindow.LoadResults(fileName);
}
}
As you see in Parent (MainWindow) I had to use static method
public static void LoadResults(string fileName)
{
string fullFileName = @"C:\Users\Public\Documents\Test Data\" + fileName;
var binFormat = new BinaryFormatter();
var testData = new TestData();
if (File.Exists(fullFileName))
{
using (Stream fStream = new FileStream(fullFileName, FileMode.Open))
{
testData = (TestData) binFormat.Deserialize(fStream);
}
}
//here I am trying to load data from testData instance of TestData class into data
//tables or set Text property of a TextBox. Can't access them from a static method!
}
I know that I shouldn't even try to access non-static members from a static method. I am just trying to explain my task. Is there any way in WPF (where class Window is defined in XAML) to access the instance of Parent window at runtime and than its methods? I don't mind to change total approach if there is more elegant and simpler solution.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
在最简单的形式中,您可以通过
Owner
属性获取对父窗口的引用,并将现有的LoadResults
方法更改为实例方法。In the very simplest form, you could obtain a reference to the parent window via the
Owner
property and change your existingLoadResults
method to be an instance method.如果您希望方法静态,只需将需要的所有参数传递给它即可。另一种选择是使您的 LoadResults 实例方法处于静态状态。
If you want your method static just pass all arguments you need to it. The other option is to make your
LoadResults
instance method instatead of static.