簡體   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