2

I am creating TabItems programmatically without any issue by using the following code:

var tabItem = new TabItem();
tabItem.Header = "My Tab Header";
tabItem.Content = new UserControl1();
MainTabControl.Items.Add(tabItem); 

Now when a tab item added to tab control i also want to add image button at the same time with the creation of TabItem aligned at right side. How can i achieve this? thanks in advance.

EDIT: I have tried a lot and still did not get an idea. following is my tabcontrol in xaml and ObservableCollection. When i run project tabs shows successfully but i don't know how to add images in it because in my tab control in xaml, it does not have TabItems markup and they are displaying automatically when running project. Please view my sample code and transform into my desired result. I wana conclude and close this issue, I Really thanks and appreciate help.

Following is xaml:

  <TabControl ItemsSource="{Binding TabItems, Mode=TwoWay}" DisplayMemberPath="Content" HorizontalAlignment="Left" Height="73" Margin="10,25,0,0" VerticalAlignment="Top" Width="312"/>

Following is viewmodel

     namespace WpfApplication1.ViewModels
    {
    public class VMTabControl : INotifyPropertyChanged
    {

        public event PropertyChangedEventHandler PropertyChanged;
        protected void OnPropertyChanged(string propertyname)
        {
            PropertyChangedEventHandler handler = PropertyChanged;
            if (handler != null)
            {
                handler(this, new PropertyChangedEventArgs(propertyname));
            }
        [![enter image description here][1]][1]}

        public VMTabControl()
        {
            TabItems = new ObservableCollection<clsTabs>(GetList().ToList());
        }

        private ObservableCollection<clsTabs> _tabItems;
        public ObservableCollection<clsTabs> TabItems
        {
            get { return _tabItems; }
            set
            {
                _tabItems = value;
                OnPropertyChanged("TabItems");
            }
        }

        public List<clsTabs> GetList()
        {
            List<clsTabs> tablist = new List<clsTabs>();
            tablist.Add(new clsTabs { Content = "First", ImgPath = "path" });
            tablist.Add(new clsTabs { Content = "Second", ImgPath = "path" });
            return tablist;
        }

    }
}
Rauf Abid
  • 313
  • 2
  • 8
  • 24
  • like a close button? – lerner1225 Nov 11 '15 at 11:59
  • Yes, i want a close button, i see some articles but i want to make it very simple just like adding TabItems programmatically above.... please help. – Rauf Abid Nov 11 '15 at 12:09
  • `TabItem.Header` isn't limited to being a string - you can use a UI control as header just as well. Note that `TabControl.ItemsSource` is bindable, so you don't necessarily need to create tab items via code. – Pieter Witvoet Nov 11 '15 at 12:11
  • Thanks pieter, can you please share any code example that can fulfill my need? thanks. – Rauf Abid Nov 11 '15 at 12:20

2 Answers2

1

In code

TabItem.Header is not limited to displaying strings - you can set any UI control on it. For example:

tabItem.Header = new Button { Content = "Click me" };

To display both text and a close button, you could use a horizontal stack panel that contains a text block and a button.

In XAML

However, UI layouts are most often written in XAML. The following XAML assumes you have an Items property in your view model, which is a collection of items. These items have a TabHeaderName and TabImagePath property. The view model should also have a RemoveTabCommand property, which is an ICommand that takes a single argument (the tab item to be removed):

<TabControl ItemsSource="{Binding Items}">
    <!-- // If you only need to display a single property, you can use DisplayMemberPath.
         // If you need something more fancy (such as a text-block and button next to each other),
         // you'll have to provide a tab header template instead: -->
    <TabControl.ItemTemplate>
        <DataTemplate>
            <!-- // Tab item header template (this is how each tab header will look): -->
            <StackPanel Orientation="Horizontal">
                <TextBlock Text="{Binding TabHeaderName}" />
                <Button Content="X"
                        Command="{Binding DataContext.RemoveTabCommand, RelativeSource={RelativeSource AncestorType=TabControl}}"
                        CommandParameter="{Binding}" />
            </StackPanel>
        </DataTemplate>
    </TabControl.ItemTemplate>
    <TabControl.ContentTemplate>
        <DataTemplate>
            <!-- // Tab item content template (this is how each tab content will look): -->
            <Image Source="{Binding TabImagePath}" />
        </DataTemplate>
    </TabControl.ContentTemplate>
</TabControl>

If Items is an observable collection, simply adding an item to it will automatically add a tab item for it. Likewise, removing an item will remove its tab item.

Pieter Witvoet
  • 2,773
  • 1
  • 22
  • 33
  • 1
    Please accept my apologies for late reply. I have edit my question please help accordingly. I am Very thankful for your time. – Rauf Abid Nov 13 '15 at 12:19
0

Try this:

    var tabItem = new TabItem();
    var stack = new StackPanel();
    var t = new TextBlock();
    t.Text = "My Tab Header";
    var i = new Image();
    //i.Source = ...
    stack.Children.Add(t);
    stack.Children.Add(i);
    tabItem.Header = stack;
    tabItem.Content = new StackPanel();
    tab.Items.Add(tabItem);
lerner1225
  • 862
  • 7
  • 25
  • Thanks lerner, your code is very close to my issue. I have two simple questions. First Ques is: I have added image and text using your code, i want to run click event when click to this image(for closing tab), How can i do this programatically?. second Ques is: plz see the code of Pieter, i also like this code but was unsuccessful to implement. In his c# How can i identify the button control with its name and assign image. I believe i have to write some to set image but don't know how to do. Really appreciate your help :). – Rauf Abid Nov 11 '15 at 17:51
  • Ques 1: Replace Image with button. http://stackoverflow.com/questions/15627123/adding-an-image-inside-a-button-programmatically. Ques 2: http://stackoverflow.com/questions/17515631/add-an-image-in-a-wpf-button. I would recommend you to go with @Pieter's answer, if you have Observable Collection. – lerner1225 Nov 12 '15 at 09:05