我在 UserControl 中放入了 2 个表单,并且有一个 Grid,我将 UserControls 添加为子项,具体取决于 ComboBox 中的选择
我需要从 UserControls 中获取 TextBoxes 的值和名称。代码:MainWindow.XAML
<Grid>
<ComboBox HorizontalAlignment="Left" VerticalAlignment="Top" Margin="10,10,0,0" Name="FormType" SelectionChanged="FormType_SelectionChanged"></ComboBox>
<Grid Name="FormContent" VerticalAlignment="Top" HorizontalAlignment="Left" Margin="10,50,0,0" Height="261" Width="770"/>
<Button Name="StartButton" HorizontalAlignment="Left" VerticalAlignment="Bottom" Margin="10,0,0,103" Content="START" Click="StartButton_Click"></Button>
<Button Name="StopButton" HorizontalAlignment="Left" VerticalAlignment="Bottom" Margin="100,0,0,103" Content="STOP" Click="StopButton_Click" IsEnabled="False"></Button>
</Grid>
主窗口.xaml.cs
public partial class MainWindow : Window
{
UserControl view_form;
string form = "yaw emulator";
string[] device_list = { "yaw emulator", "6dof" };
public MainWindow()
{
InitializeComponent();
foreach (string item in this.device_list)
{
FormType.Items.Add(item);
}
FormType.SelectedIndex = 0;
this.MakeContentForm();
}
private void MakeContentForm()
{
FormContent.Children.Clear();
if (this.form == "6dof")
{
this.view_form = new device_form_6dof();
}
else if (this.form == "yaw emulator")
{
this.view_form = new device_form_yaw_emulator();
}
else
{
this.view_form = new device_form_yaw_emulator();
}
this.view_form.Name = "View_Form";
FormContent.Children.Add(view_form);
}
private void FormType_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
string text = (sender as ComboBox).SelectedItem as string;
this.form = text;
this.MakeContentForm();
}
private void StartButton_Click(object sender, RoutedEventArgs e)
{
var list = FormContent.Children[0];
//this.player = new MovePlayer();
//this.device.start();
//this.player.Start(this.device);
this.StartButton.IsEnabled = false;
this.StopButton.IsEnabled = true;
}
用户控件之一(它们仅在字段数上有所不同)
device_form_6dof.xaml
<Grid>
<Label Content="COM port name" HorizontalAlignment="Left" Height="31" Margin="10,10,0,0" VerticalAlignment="Top" Width="101"/>
<TextBox TextAlignment="Center" Margin="116,14,0,0" TextWrapping="Wrap" Text="COM1" VerticalAlignment="Top" Width="92" HorizontalAlignment="Left" Height="20" Name="port_name" TextChanged="port_name_TextChanged"/>
<Label Content="Baudrate" HorizontalAlignment="Left" Height="28" Margin="10,41,0,0" VerticalAlignment="Top" Width="101"/>
<TextBox TextAlignment="Center" Margin="116,45,0,0" TextWrapping="Wrap" Text="9600" VerticalAlignment="Top" Width="92" HorizontalAlignment="Left" Height="20" MaxLength="6" Name="baudrate_tb" TextChanged="baudrate_tb_TextChanged"/>
</Grid>
到目前为止,在 device_form_6dof.xaml.cs 代码中只有存根 - 那里没有什么可显示的......
当按下 Start 时,我还需要获取现在处于活动状态的 UserControl 字段的数据。
在链接上找到类似的东西,但不太明白。
UPD:理论上,我在 view_form 字段中保存了我在表单上使用的 UserControl,并且可以从 MainWindow 中的任何位置访问它。理论上,这是一个UserControl类型的字段, 这里表示我可以使用Controls属性获取我需要的一切。但由于某种原因,我无法访问它......嗯......到目前为止,我看到的唯一选择是为类似的 UserControls 创建一个父类,并为所有具有值的必要字段实现返回方法......但是我不想重新发明轮子。在我看来,你需要的一切都在某个地方。
UPD2:关于控件 - 不正确。引用的 UserControl 来自 System.Windows.Forms 空间,而我是从 System.Windows.Controls 空间创建的。
解决方案:感谢 Max Zhukov 同志 - 他帮了很多忙。我在链接上找到了答案的来源。对于初学者来说,这是一种简单易懂的方法。拿他怎么办?为此,我保留了一个静态 utils 类。
namespace rServer
{
static class utils
{
public static IEnumerable<T> FindVisualChildren<T>(DependencyObject depObj) where T : DependencyObject
{
if (depObj != null)
{
for (int i = 0; i < VisualTreeHelper.GetChildrenCount(depObj); i++)
{
DependencyObject child = VisualTreeHelper.GetChild(depObj, i);
if (child != null && child is T)
{
yield return (T)child;
}
foreach (T childOfChild in FindVisualChildren<T>(child))
{
yield return childOfChild;
}
}
}
}
}
}
然后,在 MainWindow.xaml.cs 中,如有必要,我进行如下调用
foreach (TextBox tb in utils.FindVisualChildren<TextBox>('Имя_элемента_по_которому_ищем'))
{
Console.Write(tb.Name);
Console.WriteLine(tb.Text);
}