2012-05-04 57 views
0

我有一個WPF應用程序,我希望以編程方式顯示內容。但是,當我嘗試以某種順序執行此操作時,它無法顯示。以編程方式設置標籤的內容

這工作

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) 
     { 
     ........ 
     } 
} 

這並不

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); 
      ....... 
     } 
} 

爲什麼不呢?

+0

我需要重新繪製表格什麼的? – user589195

+0

使用MVVM這已經超出了微不足道的地步。投入一些時間並學習模式。 – Will

回答

1

你需要把一個StackPanel在標籤項

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

然後在後面的代碼

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) 
     { 
     } 
    } 
} 
+0

我已經試過這個,但仍然沒有看到按鈕。 – user589195

+0

@ user589195,它似乎工作正常,讓我添加完整的XAML和代碼背後 – Habib

+0

@ user589195你在哪裏添加代碼後面的按鈕,它應該是在InitializeComponent – Habib

相關問題