WPF 子窗体、主窗体传参

在主窗体里面操纵子窗体的控件

子窗体

<Label x:Name="DialogLabel"></Label>

主窗体

<Grid>
    <Grid.RowDefinitions>
        <RowDefinition Height="*"></RowDefinition>
        <RowDefinition Height="*"></RowDefinition>
    </Grid.RowDefinitions>
    <TextBox Margin="20" x:Name="InputTextBox"></TextBox>
    <Button Margin="20" Grid.Row="1" Content="打开弹窗" Click="OpenDialog"></Button>
</Grid>

主窗体点击事件操作

private void OpenDialog(object sender, RoutedEventArgs e)
{
    Dialog dialog = new Dialog();
        //在主窗体里面操纵子窗体的控件
    dialog.DialogLabel.Content = InputTextBox.Text;
    dialog.ShowDialog();
}

直接在主窗体里面操纵子窗体的控件,进行一系列操作,业务逻辑也放在主窗体

示例代码

https://github.com/zLulus/NotePractice/tree/dev3/WPF/WpfDemo/StartWindowAndDialog

原文地址:https://www.cnblogs.com/Lulus/p/8158388.html