nwrobinson
nwrobinson

Reputation: 41

WPF tabcontrol programatically changing the # of tabs, their name, and information displayed in C#

i am working on a program that parses log files and organizes them for easy viewing and error fixing. one piece of data included on the log is channel data, which shows what computer in the network that report came from. i want to display the parsed logs in a tabcontrol window, where every channel is displayed in a separate tab.

the problem is that the number of tabs in a given system (these are planetariums) range from 4-64, so instead of creating 64 tabs and most of the time only filling a few of them, i was hoping to, in the C# background code, programmatically look at the logs, creating a tab for every new channel that appears and adding that log to that tabs text. essentially the code should look something like this:

public void fillTabs()
{
    tabViewWindow win = new tabViewWindow;
    Dictionary<string,string> logs = new Dictionary<string,string>();
    foreach(var log in infoLogParser.parsedLogs)
    {
        if(logs["Channel "+log.data[2]].Exists())
        {
            logs[tab.name].Add(log.data[4] + "\n");
        }
        else
        {
            win.tabviewer.addtab();
            tab.name = "Channel " +log.data[2];
            logs.Add("channel " + log.data[2], log.data[4] + "\n");
        }
     }
 }

and then from there ill add the code to from the list to the tabs. but i cant figure out how to actually create the tabs in the TabControl.

i always forge tto add this but im working in .net 3.5 so the program will be usable on the XP systems

Upvotes: 0

Views: 204

Answers (2)

pushpraj
pushpraj

Reputation: 13669

So I'll start by creating data class for Channel

    public class Channel
    {
        public Channel()
        {
            Log = new List<string>();
        }

        public string Name { get; set; }
        public List<string> Log { get; set; }
    }

then I'll declare another class which will become DataContext for the view or you can say the Window

with a property of type ObservableCollection which will help me in binding and the intended function

    class MainViewModel
    {
        public MainViewModel()
        {
            Channels = new ObservableCollection<Channel>();
        }

        public ObservableCollection<Channel> Channels { get; set; }

        public void fillTabs()
        {
            foreach (var log in infoLogParser.parsedLogs)
            {
                Channel ch = Channels.FirstOrDefault(c => c.Name = "Channel " + log.data[2]);
                if (ch == null)
                {
                    ch = new Channel();
                    ch.Name = "Channel " + log.data[2];
                    Channels.Add(ch);
                }
                ch.Log.Add(log.data[4] + "\n");
            }
        }
    }

additionally i modified the function to work with new approach

now I'll set the instance of this class to the view or my main window's constructor, there are many ways to assign, this is just a simple one.

    public MainWindow()
    {
        InitializeComponent();
        DataContext = new MainViewModel();
    }

then in WPF XAML I'll bind the tab control's ItemSource to my collection and will create datatemplates for header and the content

    <TabControl ItemsSource="{Binding Channels}" >
        <TabControl.Resources>
            <Style TargetType="TabItem">
                <Setter Property="Header" Value="{Binding Name}" />
            </Style>
        </TabControl.Resources>
        <TabControl.ContentTemplate>
            <DataTemplate>
                <ListBox ItemsSource="{Binding Log}"/>
            </DataTemplate>
        </TabControl.ContentTemplate>
    </TabControl>

and you are done with simple binding which will show your channels as tab items in the tab control

now tab control will react to changes in the collection property Channels, if you add a channel it will be added to tabcontrol and same with removal and reordering, you just have to manipulate the collection from the code, you dont need to touch the UI elements at all.

Upvotes: 2

Daniel Ward
Daniel Ward

Reputation: 274

You can add TabItems to a TabControl in code behind like this:

TabControl tc = new TabControl();
TabItem ti = new TabItem();
tc.Items.Add(ti);

Upvotes: 0

Related Questions