locked
Set text from Page to UserControl TextBlock in metro style apps RRS feed

  • Question

  • Hi.

    I have created UserControl with TextBlock and created a page using Metro style app template. and at the run time i have loaded the usercontrol on page on grid control using GridName.Children.Add(new UserControl()).

    -Now i want set the text from the page to UserControl TextBlock.

    Please help me how it can be done.


    Ravi Shankar

    Wednesday, July 4, 2012 11:31 AM

Answers

  • Hii Use this in this method "DispalayMessageOnUserControl"

    var x = (MyUserControl)grdUserControl.Children[0];      
    var myTx = (TextBlock)x.FindName("txtBlock");
    myTx.Text = "Some Text";


    By Sanz. -- If you find this post helpful then please "Vote as Helpful" and "Mark As Answer".


    • Edited by san Sanz Wednesday, July 4, 2012 12:21 PM
    • Proposed as answer by san Sanz Wednesday, July 4, 2012 12:21 PM
    • Marked as answer by R.Shankar Wednesday, July 4, 2012 12:30 PM
    Wednesday, July 4, 2012 12:21 PM

All replies

  • Hi Ravi,

    can you please insert your code block and when to add the text at the point of load?

    or from UI?


    By Sanz. -- If you find this post helpful then please "Vote as Helpful" and "Mark As Answer".

    Wednesday, July 4, 2012 11:39 AM
  • MainPage.Xaml

    <Page
        x:Class="Test.MainPage"
        IsTabStop="false"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:local="using:Test"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        mc:Ignorable="d">

        <Grid Background="Green">
            <Grid x:Name="grdUserControl"></Grid>
        </Grid>
    </Page>

    ==============================================

    MainPage.xaml.cs

    public sealed partial class MainPage : Page
        {
            public MainPage()
            {
                this.InitializeComponent();
                loadUserControl();
                DispalayMessageOnUserControl();
            }

            private void DispalayMessageOnUserControl()
            {
                txtBlock.Text = "Some Text";
            }

            private void loadUserControl()
            {
                grdUserControl.Children.Add(new MyUserControl());
            }
        }

    =================================================

    MyUserControl.xaml 

    <UserControl
        x:Class="Test.MyUserControl"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:local="using:Test"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        mc:Ignorable="d"
        d:DesignHeight="300"
        d:DesignWidth="400">
        
        <Grid>
            <StackPanel>
                <TextBlock x:Name="txtBlock" FontSize="25"></TextBlock>
            </StackPanel>
        </Grid>
    </UserControl>

    =======================================



    Ravi Shankar

    Wednesday, July 4, 2012 12:09 PM
  • Hii Use this in this method "DispalayMessageOnUserControl"

    var x = (MyUserControl)grdUserControl.Children[0];      
    var myTx = (TextBlock)x.FindName("txtBlock");
    myTx.Text = "Some Text";


    By Sanz. -- If you find this post helpful then please "Vote as Helpful" and "Mark As Answer".


    • Edited by san Sanz Wednesday, July 4, 2012 12:21 PM
    • Proposed as answer by san Sanz Wednesday, July 4, 2012 12:21 PM
    • Marked as answer by R.Shankar Wednesday, July 4, 2012 12:30 PM
    Wednesday, July 4, 2012 12:21 PM
  • Thanks.

    It's Working.

    Can you please tell me how can we display text to any UI component from and Class or other threads?

    Regards


    Ravi Shankar

    Wednesday, July 4, 2012 12:30 PM