【问题标题】:Dynamically Accessing MainPage controls in child UserControl's from codebehind in metro application?从 Metro 应用程序中的代码动态访问子用户控件中的 MainPage 控件?
【发布时间】:2012-08-29 09:49:38
【问题描述】:

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

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

现在,View1(用户控件)有一个 button1,在该 button1 上单击我必须删除 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);
}

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

【问题讨论】:

    标签: c# wcf silverlight xaml microsoft-metro


    【解决方案1】:
    Label lbl = (Label)this.Page.FindControl("controlID");
    string labelText = lbl.Text;
    

    【讨论】:

      【解决方案2】:

      考虑使用 ContentControl 而不是这样做。 使用它,您将能够更改其内容,随心所欲地添加。

      将此添加到您的视图中:

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

      后面的代码你就可以这样做:

      region1ContentControl.Content = AnyObject(including views)
      

      这将比一直更改视图更容易工作。

      希望对你有帮助

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2015-03-30
        • 2012-09-28
        相关资源
        最近更新 更多