简体   繁体   English

C#/WPF - 从父窗口获取子用户控件内的对象

[英]C# / WPF - Getting object inside a child user control from parent window

Not terribly familiar with WPF and C# so if this is blatantly wrong, please correct me.不太熟悉 WPF 和 C#,所以如果这是公然错误的,请纠正我。 Working in VSExpress2015 .NET Framework 4.5.在 VSExpress2015 .NET Framework 4.5 中工作。 I'm heavily simplifying my code below, so know that namespace/library references are there.我在下面大大简化了我的代码,所以知道命名空间/库引用在那里。

Say I have a window with a Button and ContentControl inside:假设我有一个窗口,里面有一个 Button 和 ContentControl:

<Window x:Class="Project.MainWindow">
    <Grid>
        <Button Name="Submit_Btn" Click="Submit_Btn_Click">
        <ContentControl Name="MainContentControl">
    </Grid>
</Window>

I also have several user control files in my project with XAML that looks something like this:我的项目中还有几个带有 XAML 的用户控制文件,如下所示:

<UserControl x:Class="Project.UserControl1">
    <Grid>
        <TextBox Name="TxtBox1">
    </Grid>
</Usercontrol>

I have code in the backend of my MainWindow to dynamically load the appropriate UserControl into the "MainContentControl" object.我在 MainWindow 的后端有代码可以将适当的 UserControl 动态加载到“MainContentControl”对象中。 However, I want to reference the objects inside of the currently loaded UserControl from the MainWindow's Submit_Btn_Click function.但是,我想从 MainWindow 的 Submit_Btn_Click 函数中引用当前加载的 UserControl内部的对象。 For example, in MainWindow.cs, do something like this:例如,在 MainWindow.cs 中,执行如下操作:

private void Submit_Btn_Click(object sender, RoutedEventArgs e)
{
    if(MainContentControl is currently loaded with UserControl1)
        Do_Something_Function(MainContentControl.TxtBox1.Text);
}

The main problem here is I don't know how to call the TextBox1 element from within the parent MainWindow's scope.这里的主要问题是我不知道如何从父 MainWindow 的范围内调用 TextBox1 元素。 I'm also not sure how to validate the if condition (confirming the control is currently loaded).我也不确定如何验证 if 条件(确认控件当前已加载)。 Does anyone know of a way to think about this differently or even directly reference the object (despite that probably not being a great idea)?有没有人知道一种以不同方式思考这个甚至直接引用对象的方法(尽管这可能不是一个好主意)?

-- ——

I'm not using/familiar with MVVM at all (yet), and I'm not particularly concerned with optimal performance as this is a one off temporary project that will soon die and be re-worked.我根本不使用/不熟悉 MVVM(还),而且我并不特别关心最佳性能,因为这是一个一次性的临时项目,很快就会消亡并重新工作。 I've read ways how to access the data in a parent window from a child, but I didn't find scenarios that really matched up with this.我已经阅读了如何从孩子访问父窗口中的数据的方法,但我没有找到真正与此匹配的场景。

Once again, I'm still familiarizing myself with C#, WPF and general coding practices (it's been a couple years), so if using a ContentControl or UserControl here isn't optimal, (or mixing the two doesn't make sense) that information would be greatly appreciated;再一次,我仍然熟悉 C#、WPF 和一般编码实践(已经有几年了),所以如果在这里使用 ContentControl 或 UserControl 不是最佳的,(或者混合两者没有意义)那信息将不胜感激; however, in this scenario, I'm more concerned with just getting this working until I can learn more proper techniques later.然而,在这种情况下,我更关心的是让这个工作,直到我以后可以学习更多正确的技术。

Instead of trying to access the TextBox inside the UserControl, you can expose properties and methods on the UserControl itself to interact with what's inside.无需尝试访问 UserControl 内的 TextBox,您可以公开 UserControl 本身的属性和方法以与内部内容进行交互。 In your case, you could add a property that returns the current value of TextBox.Text.在您的情况下,您可以添加一个返回 TextBox.Text 的当前值的属性。 You can also add dependency properties to facilitate binding.您还可以添加依赖项属性以方便绑定。

You can use the LogicalTreeHelper to search by name.您可以使用LogicalTreeHelper按名称搜索。

So for your example to access TextBox1因此,对于您访问TextBox1的示例

 var txtBox = LogicalTreeHelper.FindLogicalNode(MainContentControl, "TextBox1") as TextBox;
 Do_Something_Function(txtBox?.Text);

It definitely looks like the design should be improved, but just to get this working you can do the following:看起来确实应该改进设计,但是为了让它工作,您可以执行以下操作:

    private void Submit_Btn_Click(object sender, RoutedEventArgs e)
    {
        var controlAsUserControl1 = MainContentControl.Content as UserControl1;
        if (controlAsUserControl1 != null)
        {
            Debug.WriteLine(controlAsUserControl1.TxtBox1.Text);
        }
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM