如何将数据从子表单事件传递到父表单项?

发布于 2024-10-14 02:25:33 字数 722 浏览 1 评论 0原文

目前,我有一个主窗体,我从中调用一个弹出窗口,该弹出窗口是我在单独的 XAML 文件中为拼写检查器创建的。它的调用方式如下:

SpellCheckerPopupControl popupControl = new SpellCheckerPopupControl();

popupControl.SpellChecklistBox.Items.Clear();

//some code to check the word and populate the child listbox with spelling suggestions

popupControl.SpellCheckerPopup.IsOpen = true;

我在列表框中的 SelectionChanged 子项中设置了一个事件,以便将数据传递回主窗体,目前我只想将选择传递回主窗体,以便我可以获取所选内容单词并用它替换拼写错误的单词。不幸的是,我似乎无法将数据从子窗体的事件传递回父窗体。

SpellCheckerPopup 的 XAML 只是一个 ,其中包含一个

仅出于测试目的,我尝试仅获取所发生情况的输出并将字符串粘贴到主文本框中,如下所示:MainPage.txtArticle.Text = s;,但是虽然 MainPage 位是可用,但它的所有元素都不可用,因此我无法从子事件中操纵它们。

Currently I have a main form, from which I call a popup that I've created in a seperate XAML file for my spellchecker. It's called like so:

SpellCheckerPopupControl popupControl = new SpellCheckerPopupControl();

popupControl.SpellChecklistBox.Items.Clear();

//some code to check the word and populate the child listbox with spelling suggestions

popupControl.SpellCheckerPopup.IsOpen = true;

I have an event setup in the child on SelectionChanged in the listbox for it to pass the data back to the main form, currently I just want to pass the selection back to my main form so that I can take the selected word and replace the misspelled one with it. Unfortunately, I don't seem to be able to pass data from the child form's events back to the parent.

The XAML for the SpellCheckerPopup is just a <PopUp> with a <ListBox> inside it.

Just for testing purposes, I've tried to just take the output of what happens and paste the string into the main textbox like so: MainPage.txtArticle.Text = s;, but while the MainPage bit is available, none of its elements are, and as such I cannot manipulate them from the child event.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

痞味浪人 2024-10-21 02:25:33

创建窗口的属性

var window = new popupControl.Open();

,然后在 window.close 事件中检查 Window.txtArticle.Text;

这应该有效。如果没有,则将它们绑定到后面代码中的属性。

[更新]

示例:

var window = new MyTestChildWindow();
//This is just to show how to change properties in the testwindow.
window.SomeProperty = true;
window.Show();
window.Closed += (s, ea) =>
{
  if (window.DialogResult.ToBoolean())
  {
    // this is a property in the code behind.
    if (window.SelectedItem != null)
    {
      //Property = property on mainpage
      //window.SelectedItem = property in childwindow
      this.Property = window.SelectedItem;
    }
  }
};

此代码位于您的主页中。

希望这有帮助。

Make a property of the window

var window = new popupControl.Open();

then on window.closed event check for Window.txtArticle.Text;

This should work. If not, then bind them to properties in the code behind.

[update]

Example:

var window = new MyTestChildWindow();
//This is just to show how to change properties in the testwindow.
window.SomeProperty = true;
window.Show();
window.Closed += (s, ea) =>
{
  if (window.DialogResult.ToBoolean())
  {
    // this is a property in the code behind.
    if (window.SelectedItem != null)
    {
      //Property = property on mainpage
      //window.SelectedItem = property in childwindow
      this.Property = window.SelectedItem;
    }
  }
};

this code goes in your main page.

hope this helps.

妞丶爷亲个 2024-10-21 02:25:33

您还可以在 Program.cs 文件中创建一个可以向其传递值的属性。然后您就可以在整个应用程序范围内访问该属性。只需使用:

Program.SomeProperty

You could also create a property in your Program.cs file that you could pass a value to. Then you would have access to that property application wide. Just use:

Program.SomeProperty
琴流音 2024-10-21 02:25:33

使用在其他地方声明的变量会引发问题,因为没有任何东西可以保证“外部”数据的真实性。

如果您的事件是路由事件,您始终可以查询 OriginalSource 并从中获取信息。如果您的事件不是路由事件,您可以创建一个附加事件并向弹出窗口传递 OriginalSource。

Using variables, declared somewhere else is a recipe for problem as nothing will gurantee actuality of that 'external' data.

Provided your event is a routed one, you can always query for OriginalSource and obtain information from it. If your event is not a routed one, you can create an attached event and pass your popup a OriginalSource.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文