我有WPF用户控件中的2个文本框和WPF表单上的按钮.如何在我使用WPF用户控件的主窗体的按钮单击事件上访问这些文本框值
首先,请记住,WPF是不是的WinForms -理论上你应该将数据绑定你的文本框,以属性,然后更改,而不是直接访问文本框属性的值,!
话虽这么说,你要做的就是命名UserControl和TextBoxes,然后访问它们,如下所示:
Int MyUserControl.xaml:
<TextBox x:Name="myTextBox1"/>
<TextBox x:Name="myTextBox2"/>
Run Code Online (Sandbox Code Playgroud)
在MyWindow.xaml中:
<local:MyUserControl x:Name="myUserControlInstance"/>
<Button Content="Click me" Click="Button_Click" />
Run Code Online (Sandbox Code Playgroud)
在MyWindow.xaml.cs中:
private void Button_Click(object sender, RoutedEventArgs e) {
myUserControlInstance.myTextBox1.Text = "Foo";
myUserControlInstance.myTextBox2.Text = "Bar";
}
Run Code Online (Sandbox Code Playgroud)