如何将数据从子表单事件传递到父表单项?
目前,我有一个主窗体,我从中调用一个弹出窗口,该弹出窗口是我在单独的 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
创建窗口的属性
,然后在 window.close 事件中检查
Window.txtArticle
.Text;这应该有效。如果没有,则将它们绑定到后面代码中的属性。
[更新]
示例:
此代码位于您的主页中。
希望这有帮助。
Make a property of the window
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:
this code goes in your main page.
hope this helps.
您还可以在 Program.cs 文件中创建一个可以向其传递值的属性。然后您就可以在整个应用程序范围内访问该属性。只需使用:
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:
使用在其他地方声明的变量会引发问题,因为没有任何东西可以保证“外部”数据的真实性。
如果您的事件是路由事件,您始终可以查询 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.