locked
how to add item in stackpanel at runtime ,when page orientation is set to landscape RRS feed

  • Question

  • HI

    I am trying to add the item in stack panel at runtime when my page orientaion is set to portrait mode.

    My page orientation and supported orientation is supported to landscape mode

    Here is my Xaml Code

    <phone:PhoneApplicationPage 
        x:Class="Hunt.Setting"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:phone="clr-namespace:Microsoft.Phone.Controls;assembly=Microsoft.Phone"
        xmlns:shell="clr-namespace:Microsoft.Phone.Shell;assembly=Microsoft.Phone"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        FontFamily="{StaticResource PhoneFontFamilyNormal}"
        FontSize="{StaticResource PhoneFontSizeNormal}"
        Foreground="{StaticResource PhoneForegroundBrush}"
        SupportedOrientations="Landscape" Orientation="Landscape"
        mc:Ignorable="d" d:DesignHeight="480" d:DesignWidth="800"
        shell:SystemTray.IsVisible="False">
    
        <!--LayoutRoot is the root grid where all page content is placed-->
        <Grid x:Name="LayoutRoot" Background="Transparent">
            <Grid.RowDefinitions>
                <RowDefinition Height="Auto"/>
                <RowDefinition Height="*"/>
            </Grid.RowDefinitions>
    
            <!--TitlePanel contains the name of the application and page title-->
            <StackPanel x:Name="TitlePanel" Grid.Row="0" Margin="12,17,0,28">
                <TextBlock x:Name="ApplicationTitle" Text="MY APPLICATION" Style="{StaticResource PhoneTextNormalStyle}"/>
                <TextBlock x:Name="PageTitle" Text="page name" Margin="9,-7,0,0" Style="{StaticResource PhoneTextTitle1Style}"/>
            </StackPanel>
    
            <!--ContentPanel - place additional content here-->
            <Grid x:Name="ContentPanel" Grid.Row="1" Margin="12,0,12,0">
                <Button Content="Button" Height="91" HorizontalAlignment="Left" Margin="86,83,0,0" Name="button1" VerticalAlignment="Top" Width="191" Click="button1_Click" />
            </Grid>
        </Grid>
     
    </phone:PhoneApplicationPage>
    

    And here is my Code behind

    public partial class Setting : PhoneApplicationPage
        {
            Popup p = new Popup();
            public Setting()
            {
                InitializeComponent();
            }
    
            private void button1_Click(object sender, RoutedEventArgs e)
            {
                this.SupportedOrientations = SupportedPageOrientation.Landscape;
                this.Orientation = PageOrientation.Landscape;
                Border border = new Border();
                border.BorderBrush = new SolidColorBrush(Colors.Black);
                border.BorderThickness = new Thickness(5.0);
                border.Background = new SolidColorBrush(Colors.Magenta);
                border.Height = 800;
                border.Width = 480;
    
    
                StackPanel panel1 = new StackPanel();
                panel1.Width = 480;
                panel1.Height = 480;
                panel1.Background = new SolidColorBrush(Colors.Green);
                panel1.Orientation = System.Windows.Controls.Orientation.Vertical;
    
                Button button1 = new Button();
                button1.Content = "Close";
    
                button1.Click += new RoutedEventHandler(Onclick);
                TextBlock textblock1 = new TextBlock();
                textblock1.Text = "The popup control";
                textblock1.Margin = new Thickness(0, 10, 0, 0);
                panel1.Children.Add(textblock1);
                panel1.Children.Add(button1);
                border.Child = panel1;
    
                // Set the Child property of Popup to the border 
                // which contains a stackpanel, textblock and button.
                p.Child = border;
    
                // Set where the popup will show up on the screen.
                //p.VerticalOffset = 0;
                //p.HorizontalOffset = 2;
    
                // Open the popup.
                p.IsOpen = true;
            }
            void Onclick(object sender, RoutedEventArgs e)
            {
                // Close the popup.
                p.IsOpen = false;
    
            }
    
        }

    Items are added succuessfylly ,but item are not oriented to landscape mode,they are getting oriented in vertical direction according  to portrait mode

    I want ,they should oriented in the vertical direction in landscape mode .

    Here is the sample outputimage not available

    any idea,about how to change the orientation in vertical direction ,when page should work in only landscape only.....

    Monday, August 20, 2012 4:10 AM

Answers

  • You created a Popup separate from the page. It will be "floating" and won't inherit the landscape orientation of the page until you add it to the page's visual tree. The easiest thing to do is add the Popup as a child of LayoutRoot.

    There are cases where you might not want to add the Popup to the page, such as a global Popup that can be displayed independently from the current page. In that case, you would have to rotate the Popup yourself to match the current phone orientation, such as with a RotateTransform.


    Richard Woo
    • Proposed as answer by sm_on_live Wednesday, August 22, 2012 1:33 PM
    • Marked as answer by Ramakant Verma Wednesday, August 22, 2012 3:27 PM
    Monday, August 20, 2012 5:56 AM

All replies

  • You created a Popup separate from the page. It will be "floating" and won't inherit the landscape orientation of the page until you add it to the page's visual tree. The easiest thing to do is add the Popup as a child of LayoutRoot.

    There are cases where you might not want to add the Popup to the page, such as a global Popup that can be displayed independently from the current page. In that case, you would have to rotate the Popup yourself to match the current phone orientation, such as with a RotateTransform.


    Richard Woo
    • Proposed as answer by sm_on_live Wednesday, August 22, 2012 1:33 PM
    • Marked as answer by Ramakant Verma Wednesday, August 22, 2012 3:27 PM
    Monday, August 20, 2012 5:56 AM
  • Thanx......

    Its solved my problem......

    Wednesday, August 22, 2012 3:37 PM