我有一个UserControl作为Parent,我打开一个自定义对话框(用户控件),如下面的代码所示:
Window dialog = new Window
{
Title = "A Dialog Box",
Content = new UserControlDialog(),
SizeToContent = SizeToContent.WidthAndHeight,
ResizeMode = ResizeMode.NoResize
};
dialog.ShowDialog();
如何从UserControl对话框中的文本框中获取文本?
答案 0 :(得分:0)
首先,在UserControlDialog
控件中创建一个公共属性,让我们说名为Value
。当用户在对话框中执行某些操作时,请将值从TextBox
设置为Value
属性:
Value = SomeTextBox.Text;
关闭对话窗口后,只需读取dialog
控件中的值:
...
dialog.ShowDialog();
string result = dialog.Value;
更好的解决方案是将Value
属性数据绑定到内部TextBox
控件,但要执行此操作,您需要实现INotifyPropertyChanged
Interface,或者将Value
属性创建为DependencyProperty
:
在UserControlDialog
控件内:
<TextBox Text="{Binding Value, RelativeSource={RelativeSource
FindAncestor={x:Type YourLocalPrefix:UserControlDialog}}}" ... />