using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Threading;
namespace WpfApplication1
{
///
/// Interaction logic for Window1.xaml
///
public partial class Window1 : Window
{
private delegate void updateProgress();
TabItem item = null;
// Used to create a TabItem control in Background thread
TabItemHelper tabItemHelper = null;
public Window1()
{
InitializeComponent();
}
private void button1_Click(object sender, RoutedEventArgs e)
{
//start thread
Thread thread = new Thread(new ThreadStart(doSomething));
thread.SetApartmentState(ApartmentState.STA);
thread.Start();
}
void doSomething()
{
updateProgress add = new updateProgress(addToMainTab);
// Create the TabItemHelper class
tabItemHelper = new TabItemHelper();
tabItemHelper.Header = "New Tab Item";
tabItemHelper.Name = "NTI1";
tabItemHelper.HorizontalAlignment = HorizontalAlignment.Right;
tabItemHelper.FontFamily = new FontFamily("Tahome");
tabItemHelper.FontSize = 24.0;
//TabControl on UI thread
tabMain.Dispatcher.Invoke(System.Windows.Threading.DispatcherPriority.Normal, add);
}
private void addToMainTab()
{
//this will succeed
((TabItem)tabMain.Items[0]).Header = "just changed it";
// Use the tabItemHelper class to populate the new TabItem control
item = new TabItem() { Header = tabItemHelper.Header, Name = tabItemHelper.Name,
HorizontalAlignment = tabItemHelper.HorizontalAlignment, FontFamily = tabItemHelper.FontFamily,
FontSize = tabItemHelper.FontSize };
//this will succeed
tabMain.Items.Add(item);
}
}
}
class TabItemHelper
{
private String header = String.Empty;
private String name = String.Empty;
private HorizontalAlignment horizontalAlignment;
private FontFamily fontFamily;
private Double fontSize = 0;
public String Header
{
get { return header; }
set { header = value; }
}
public String Name
{
get { return name; }
set { name = value; }
}
public HorizontalAlignment HorizontalAlignment
{
get { return horizontalAlignment; }
set { horizontalAlignment = value; }
}
public FontFamily FontFamily
{
get { return fontFamily; }
set { fontFamily = value; }
}
public Double FontSize
{
get { return fontSize; }
set { fontSize = value; }
}
}
|