繁体   English   中英

从Metro应用程序中的代码隐藏动态访问子UserControl中的MainPage控件?

[英]Dynamically Accessing MainPage controls in child UserControl's from codebehind in metro application?

我有一个MainPage,在屏幕的一部分中,我有一个名为customview的空网格,在其中我想动态添加和删除不同的视图(不同的usercontrols)。 现在我以这种方式将一个usercontrol(view1)附加到该空网格(MainPage的一部分)上:-customview是一个空网格,view1是我设计的用户控件,并导航到MainPage上,我正在这样做:-

protected override void OnNavigatedTo(NavigationEventArgs e)
{
customview.Children.Clear();
View1 firstview = new View1 ();
customview.Children.Add(firstview); 
}

现在,View1(用户控件)具有一个button1,在该按钮上单击i,我必须删除view1并将view2(另一个用户控件)添加到MainPage中存在的名为customview的同一网格中。

我以这种方式尝试过但没有运气:-

private void button1_Click_1(object sender, RoutedEventArgs e)
{
MainPage main = new MainPage();
View2 secview = new View2 ();
Grid grd = main.FindName("customview") as Grid;
grd .Children.Clear();
grd .Children.Add(secview);
}

请让我知道我在哪里做错了? 提前致谢。

Label lbl = (Label)this.Page.FindControl("controlID");
string labelText = lbl.Text;

考虑使用ContentControl而不是这样做。 使用它,您可以更改其内容,并在任何需要的地方添加。

添加到您的视图:

            <ContentControl Name="region1ContentControl" 
                        Grid.Row="1"
                        Grid.Column="1"
                        Margin="0,10"                           
                        Style="{StaticResource ContentControlStyle}" />

因此您可以在后面的代码中执行以下操作:

region1ContentControl.Content = AnyObject(including views)

这比一直更改视图要容易得多。

希望能帮助到你

暂无
暂无

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

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