Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. WPF
  4. How to activate the tab in tab control by select the business object

How to activate the tab in tab control by select the business object

Scheduled Pinned Locked Moved WPF
questionwpfwcfbusinesstutorial
5 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • L Offline
    L Offline
    Loveisasea
    wrote on last edited by
    #1

    as far as i know, we can not bind the collection to the TabControl.ItemSources directly. we need to convert the bussinessobject collection to tabitem collection first and bind them to the tabcontrol.itemssources. Here comes the question I can not binding the TabControl.SelectedItem to the SelectedBussinessObject. If I do so like :

    <controls:TabControl Name="tbcFactories" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" DataContext="{Binding Mode=OneWay}"
    ItemsSource="{Binding Path=Factories,Mode=OneWay, Converter={StaticResource FactorysToTabitemsConverter}}"
    SelectedItem="{Binding Path=CurrentManuFactory, Mode=OneWay, Converter={StaticResource FactoryToTabitemConverter}}" TabStripPlacement="Left"

    > I can bind the converted tabeitem collection to tabcontrol.itemssource. But the tab doesn't switch when I change the CurrentManuFactory,and what's more , the FactoryToTabitemConverter is not activated also. I am sure the CurrentManuFactory is changed and notification is send. Any tips ? :)

    J W 2 Replies Last reply
    0
    • L Loveisasea

      as far as i know, we can not bind the collection to the TabControl.ItemSources directly. we need to convert the bussinessobject collection to tabitem collection first and bind them to the tabcontrol.itemssources. Here comes the question I can not binding the TabControl.SelectedItem to the SelectedBussinessObject. If I do so like :

      <controls:TabControl Name="tbcFactories" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" DataContext="{Binding Mode=OneWay}"
      ItemsSource="{Binding Path=Factories,Mode=OneWay, Converter={StaticResource FactorysToTabitemsConverter}}"
      SelectedItem="{Binding Path=CurrentManuFactory, Mode=OneWay, Converter={StaticResource FactoryToTabitemConverter}}" TabStripPlacement="Left"

      > I can bind the converted tabeitem collection to tabcontrol.itemssource. But the tab doesn't switch when I change the CurrentManuFactory,and what's more , the FactoryToTabitemConverter is not activated also. I am sure the CurrentManuFactory is changed and notification is send. Any tips ? :)

      J Offline
      J Offline
      John ph
      wrote on last edited by
      #2

      Mode=OneWay in the SelectedItem is causing the problem, i guess. Remove that and try.

      - Regards -
         J O N


      A good thing is a bad thing if it keeps you from the best thing. - Dr. Adrian Rogers


      1 Reply Last reply
      0
      • L Loveisasea

        as far as i know, we can not bind the collection to the TabControl.ItemSources directly. we need to convert the bussinessobject collection to tabitem collection first and bind them to the tabcontrol.itemssources. Here comes the question I can not binding the TabControl.SelectedItem to the SelectedBussinessObject. If I do so like :

        <controls:TabControl Name="tbcFactories" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" DataContext="{Binding Mode=OneWay}"
        ItemsSource="{Binding Path=Factories,Mode=OneWay, Converter={StaticResource FactorysToTabitemsConverter}}"
        SelectedItem="{Binding Path=CurrentManuFactory, Mode=OneWay, Converter={StaticResource FactoryToTabitemConverter}}" TabStripPlacement="Left"

        > I can bind the converted tabeitem collection to tabcontrol.itemssource. But the tab doesn't switch when I change the CurrentManuFactory,and what's more , the FactoryToTabitemConverter is not activated also. I am sure the CurrentManuFactory is changed and notification is send. Any tips ? :)

        W Offline
        W Offline
        Wayne Gaylard
        wrote on last edited by
        #3

        You can bind a Tabcontrol to a collection of Business objects. I tried this as an excercise. My Models

        public class Factory
        {
        public string Name { get; set; }
        }
        public class Factories : ObservableCollection<Factory>
        {
        public Factories()
        {
        for (int i = 0; i < 5; i++)
        {
        Factory factory = new Factory() { Name = "Factory " + i.ToString() };
        this.Add(factory);
        }
        }
        }

        My code behind:

        public Factories AllFactories { get; set; }
        Factory currentFactory;
        public Factory CurrentFactory
        {
        get
        {
        return currentFactory;
        }
        set
        {
        currentFactory = value;
        NotifyPropertyChanged("CurrentFactory");
        }
        }

            public MainWindow()
            {
                InitializeComponent();
                AllFactories = new Factories();
                this.DataContext = this;
            }
        
            public event PropertyChangedEventHandler PropertyChanged;
            public void NotifyPropertyChanged(String info)
            {
                PropertyChangedEventHandler handler = PropertyChanged;
        
                if (handler != null)
                {
                    handler(this, new PropertyChangedEventArgs(info));
                }
            }
        

        and My XAML

        <Grid>
        <Grid.RowDefinitions>
        <RowDefinition Height="Auto"/>
        <RowDefinition />
        </Grid.RowDefinitions>
        <Grid.Resources>
        <DataTemplate DataType="{x:Type local:Factory}">
        <TextBlock Text="{Binding Path=Name}"/>
        </DataTemplate>
        </Grid.Resources>
        <ComboBox ItemsSource="{Binding Path=AllFactories}" SelectedItem="{Binding Path=CurrentFactory, Mode=TwoWay}"/>
        <TabControl Name="tcMain" Grid.Row="1" DataContext="{Binding}" ItemsSource="{Binding Path=AllFactories}"
        IsSynchronizedWithCurrentItem="True" SelectedItem="{Binding Path=CurrentFactory, Mode=TwoWay}">
        <TabControl.ItemTemplate>
        <DataTemplate>
        <DockPanel >
        <Labe

        L 1 Reply Last reply
        0
        • W Wayne Gaylard

          You can bind a Tabcontrol to a collection of Business objects. I tried this as an excercise. My Models

          public class Factory
          {
          public string Name { get; set; }
          }
          public class Factories : ObservableCollection<Factory>
          {
          public Factories()
          {
          for (int i = 0; i < 5; i++)
          {
          Factory factory = new Factory() { Name = "Factory " + i.ToString() };
          this.Add(factory);
          }
          }
          }

          My code behind:

          public Factories AllFactories { get; set; }
          Factory currentFactory;
          public Factory CurrentFactory
          {
          get
          {
          return currentFactory;
          }
          set
          {
          currentFactory = value;
          NotifyPropertyChanged("CurrentFactory");
          }
          }

              public MainWindow()
              {
                  InitializeComponent();
                  AllFactories = new Factories();
                  this.DataContext = this;
              }
          
              public event PropertyChangedEventHandler PropertyChanged;
              public void NotifyPropertyChanged(String info)
              {
                  PropertyChangedEventHandler handler = PropertyChanged;
          
                  if (handler != null)
                  {
                      handler(this, new PropertyChangedEventArgs(info));
                  }
              }
          

          and My XAML

          <Grid>
          <Grid.RowDefinitions>
          <RowDefinition Height="Auto"/>
          <RowDefinition />
          </Grid.RowDefinitions>
          <Grid.Resources>
          <DataTemplate DataType="{x:Type local:Factory}">
          <TextBlock Text="{Binding Path=Name}"/>
          </DataTemplate>
          </Grid.Resources>
          <ComboBox ItemsSource="{Binding Path=AllFactories}" SelectedItem="{Binding Path=CurrentFactory, Mode=TwoWay}"/>
          <TabControl Name="tcMain" Grid.Row="1" DataContext="{Binding}" ItemsSource="{Binding Path=AllFactories}"
          IsSynchronizedWithCurrentItem="True" SelectedItem="{Binding Path=CurrentFactory, Mode=TwoWay}">
          <TabControl.ItemTemplate>
          <DataTemplate>
          <DockPanel >
          <Labe

          L Offline
          L Offline
          Loveisasea
          wrote on last edited by
          #4

          Thank you ! I will have a try later. Btw, Mine is Silverlight. your example is silverlight or WPF ?

          W 1 Reply Last reply
          0
          • L Loveisasea

            Thank you ! I will have a try later. Btw, Mine is Silverlight. your example is silverlight or WPF ?

            W Offline
            W Offline
            Wayne Gaylard
            wrote on last edited by
            #5

            It's WPF.

            When I was a coder, we worked on algorithms. Today, we memorize APIs for countless libraries — those libraries have the algorithms - Eric Allman

            1 Reply Last reply
            0
            Reply
            • Reply as topic
            Log in to reply
            • Oldest to Newest
            • Newest to Oldest
            • Most Votes


            • Login

            • Don't have an account? Register

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • World
            • Users
            • Groups