user589195
user589195

Reputation: 4240

Programmatically setting content of tab

I have a WPF application and I want to programmatically display content. But when I try and do this in a certain order It fails to display.

This works

public partial class MainWindow : Window
    {
        private static GX3Logger logger = GX3Logger.GetLogger();
        private GX3ClientMain.GX3ClientMain clientMain = null;

        /// <summary>
        ///  Main method
        /// </summary>
        /// <param name="args">
        ///  Application args
        /// </param>
        /// 
        public MainWindow()
        {
            InitializeComponent();
            Button btn = new Button();
            btn.Content = "Moo";
            btn.Height = 100;
            btn.Width = 100;
            stackPanel1.Children.Add(btn);
        }



        public int Initialise(string[] args)
        {
        ........
        }
}

This doesn't

public partial class App : Application
    {
        void app_Startup(object sender, StartupEventArgs e)
        {
            MainWindow mw = new MainWindow();
            mw.Initialise(e.Args);

        }
    }

public partial class MainWindow : Window
    {
        private static GX3Logger logger = GX3Logger.GetLogger();
        private GX3ClientMain.GX3ClientMain clientMain = null;

        /// <summary>
        ///  Main method
        /// </summary>
        /// <param name="args">
        ///  Application args
        /// </param>
        /// 
        public MainWindow()
        {
            InitializeComponent();            
        }



        public int Initialise(string[] args)
        {
            Button btn = new Button();
            btn.Content = "Moo";
            btn.Height = 100;
            btn.Width = 100;
            stackPanel1.Children.Add(btn);
            .......
        }
}

Why not?

Upvotes: 0

Views: 106

Answers (1)

Habib
Habib

Reputation: 223257

you need to put a StackPanel in tab item

<Window x:Class="WpfApplication1.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="350" Width="525" Loaded="Window_Loaded">
        <TabControl>
            <TabItem Name="tab1">
                <StackPanel Name="stackPanel1">

                </StackPanel>
            </TabItem>
        </TabControl>
</Window>

then in code behind

namespace WpfApplication1
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
            Button btn = new Button();
            btn.Content = "Moo";
            stackPanel1.Children.Add(btn);
            Button btn2 = new Button();
            btn2.Content = "test";
            stackPanel1.Children.Add(btn2);

        }

        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
        }
    }
}

Upvotes: 1

Related Questions