从 WPF 中的窗口更改 UserControl 上文本框的文本值 [英] Change a textbox's text value on a UserControl from a Window in WPF
问题描述
我有一个 UserControl(称为 Invoice),带有一个文本框 (txtReferenceCode),它托管在 MainWindow 的 TabControl (myTabControl) 中.我从 UserControl 调用一个包含库存项目列表的窗口 (SearchWindow).该窗口需要向 UserControl 包含的文本框返回一个字符串值.我无法从窗口访问 UserControl 上的文本框,因此无法将字符串值从窗口传递给文本属性.
I have a UserControl (called Invoice) with a textbox (txtReferenceCode) hosted in a TabControl (myTabControl) on MainWindow. From the UserControl I call a window (SearchWindow) which contains a list of stock items. The window needs to return a string value to the textbox contained by the UserControl. I cannot access the textbox on the UserControl from the window and thus cannot pass the string value from the window to the text property.
UserControl 是一个作为新 tabItem 加载的实例(可能有许多作为 tabitems 的内容打开.)我只需要影响 UserControl 的当前 tabitem 实例.
The UserControl is an instance loaded as a new tabItem (there may be many open as content of tabitems.) I need to only affect the current tabitem instance of the UserControl.
例如:(SearchWindow 中的按钮点击事件)
Eg: (Button Click Event in SearchWindow)
Invoice.txtReferenceCode.Text = SearchWindow.txtReferenceCode.Text
我需要一个简单、简单的解决方案,最好是在 VB 中(但我会很乐意使用 C#).
I need a simple uncomplicated, solution preferably in VB (but I'll take C# gladly).
推荐答案
我明白了!我在此处为任何遇到此问题的人发布解决方案.
I got it! I am posting the solution here for any who struggle with this issue.
<UserControl x:Class="Invoice"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
mc:Ignorable="d"
d:DesignHeight="300" d:DesignWidth="300">
<TextBox x:Name="txtReferenceCode" Width=100 />
</UserControl>
WPF 窗口
<Window x:Class="SearchWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Height="300" Width="300">
<TextBox X:Name="TextToChangeTextBox" Width=100 />
</Window>
<小时>
背后的代码
向窗口添加属性
Class SearchWindow
Public ReadOnly Property TextValue
Get
Return TextToChangeTextBox.Text
End Get
End Property
...
End Class
现在您可以使用窗口中的属性将字符串传递给 UserControl 上的 TextBox.
Public Class Invoice
Private Sub SetValueToTextBox
Dim win As New SearchWindow
win.ShowDialog()
txtReferenceCode.Text = win.TextValue
End Sub
...
End Class
*
*
这篇关于从 WPF 中的窗口更改 UserControl 上文本框的文本值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!