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 avoid repeating dynamic tabitem at runtime?

How to avoid repeating dynamic tabitem at runtime?

Scheduled Pinned Locked Moved WPF
csharpwpfhelpquestionlinq
6 Posts 2 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
    LAPEC
    wrote on last edited by
    #1

    Hello Everyone I have created a small project in WPF. In this project I have a window with Menu Bar such as: Administration, Explorers, Reports. Each of these menus have sub menus: for instance under Administration Menu-Bar there is a sub-menu called Users, and under Explorers Menu-Bar there is a sub-menu called Menu Categories. My problem to my question is that on any of the sub-menus I click, it creates a dynamic tabitem and places them in the main tabcontrol (which this is fine), but if i click again on the same sub menus it repeats the same tabitems. Can someone please help me solve this problem. the source code for BackOfficeWindow.xaml.cs

    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.Collections.ObjectModel;
    using iPosCloud.com.iposcloud.config.uiC_;
    using iPosCloud.com.iposcloud.bo.uiC_.explorer;
    using iPosCloud.com.iposcloud.bo.uiC_.explorer.ViewModel;

    namespace iPosCloud.com.iposcloud.bo.uiC_
    {
    /// /// Interaction logic for BackOfficeWindow.xaml
    ///
    public partial class BackOfficeWindow : Window
    {
    private VMBackOfficeWindow vmBackOfficeWindow;

        public BackOfficeWindow()
        {
            InitializeComponent();
            // create an object of ViewModel as set it as DataContext
            vmBackOfficeWindow = new VMBackOfficeWindow();
            this.DataContext = vmBackOfficeWindow;
            ObservableCollectionTabItems = new ObservableCollection();
        }
    
        public ObservableCollection ObservableCollectionTabItems { get; set; }
    
        // ADMINISTRATION MENU
        private void menuItemAdminConfiguration\_Click(object sender, RoutedEventArgs e)
        {
            RestaurantConfigurationView rcv = new RestaurantConfigurationView();
            //bow.Owner = this;
            rcv.ShowDialog();
            //this.Close();
        }
    
        #region "Close TabItems"
    
        private void menuItem1\_Click(object sender, RoutedEventArgs e)
        {
            vmBackOfficeWindow.CloseTabItem(sender);
            menuUsers.IsEnabled = true;
            menuCategories.IsEnabled = true;
    
    P 1 Reply Last reply
    0
    • L LAPEC

      Hello Everyone I have created a small project in WPF. In this project I have a window with Menu Bar such as: Administration, Explorers, Reports. Each of these menus have sub menus: for instance under Administration Menu-Bar there is a sub-menu called Users, and under Explorers Menu-Bar there is a sub-menu called Menu Categories. My problem to my question is that on any of the sub-menus I click, it creates a dynamic tabitem and places them in the main tabcontrol (which this is fine), but if i click again on the same sub menus it repeats the same tabitems. Can someone please help me solve this problem. the source code for BackOfficeWindow.xaml.cs

      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.Collections.ObjectModel;
      using iPosCloud.com.iposcloud.config.uiC_;
      using iPosCloud.com.iposcloud.bo.uiC_.explorer;
      using iPosCloud.com.iposcloud.bo.uiC_.explorer.ViewModel;

      namespace iPosCloud.com.iposcloud.bo.uiC_
      {
      /// /// Interaction logic for BackOfficeWindow.xaml
      ///
      public partial class BackOfficeWindow : Window
      {
      private VMBackOfficeWindow vmBackOfficeWindow;

          public BackOfficeWindow()
          {
              InitializeComponent();
              // create an object of ViewModel as set it as DataContext
              vmBackOfficeWindow = new VMBackOfficeWindow();
              this.DataContext = vmBackOfficeWindow;
              ObservableCollectionTabItems = new ObservableCollection();
          }
      
          public ObservableCollection ObservableCollectionTabItems { get; set; }
      
          // ADMINISTRATION MENU
          private void menuItemAdminConfiguration\_Click(object sender, RoutedEventArgs e)
          {
              RestaurantConfigurationView rcv = new RestaurantConfigurationView();
              //bow.Owner = this;
              rcv.ShowDialog();
              //this.Close();
          }
      
          #region "Close TabItems"
      
          private void menuItem1\_Click(object sender, RoutedEventArgs e)
          {
              vmBackOfficeWindow.CloseTabItem(sender);
              menuUsers.IsEnabled = true;
              menuCategories.IsEnabled = true;
      
      P Offline
      P Offline
      Pete OHanlon
      wrote on last edited by
      #2

      In the code where you are attempting to add the tab, simply check to see if a tab of this name already exists before you add it.

      Chill _Maxxx_
      CodeStash - Online Snippet Management | My blog | MoXAML PowerToys | Mole 2010 - debugging made easier

      L 1 Reply Last reply
      0
      • P Pete OHanlon

        In the code where you are attempting to add the tab, simply check to see if a tab of this name already exists before you add it.

        Chill _Maxxx_
        CodeStash - Online Snippet Management | My blog | MoXAML PowerToys | Mole 2010 - debugging made easier

        L Offline
        L Offline
        LAPEC
        wrote on last edited by
        #3

        Hello Pete Thanks for replying to my question. According to your answer, there isn't any tab-item with the same name, those tab-items are created at run-time. here is the xaml code of the BackOfficeWindow.xaml

        P 1 Reply Last reply
        0
        • L LAPEC

          Hello Pete Thanks for replying to my question. According to your answer, there isn't any tab-item with the same name, those tab-items are created at run-time. here is the xaml code of the BackOfficeWindow.xaml

          P Offline
          P Offline
          Pete OHanlon
          wrote on last edited by
          #4

          The tabs are there - all you need to do is search through your ObservableCollection of tabs and see if there's one of the type you want to create. That's what I was alluding to. A naive implementation would be something like this:

          if (viewType == "Users")
          {
          foreach (var tab in ObservableCollectionTabItems)
          {
          if (tab is VMUserExplorer)
          return;
          }
          // The rest of your processing occurs here.
          }

          You could simplify this somewhat so that your AddTab and constructor looks like this :

          private Dictionary viewsDictionary = new Dictionary();
          public VMBackOfficeWindow()
          {
          ObservableCollectionTabItems = new ObservableCollection();
          viewsDictionary.Add("Users", new VMUserExplorer());
          viewsDictionary.Add("Category Explorer", new VMCategoryExplorer());
          }
          public void AddTab(string viewType)
          {
          VMParentForViews vm = viewsDictionary[viewType];
          Type vmType = vm.GetType();
          foreach (var tab in ObservableCollectionTabItems)
          {
          if (tab.GetType() == vmType)
          return;
          }
          ObservableCollectionTabItems.Add(vm);
          // Carry on processing here.
          }

          Again, it's still a relatively naive solution, and relies an awful lot on things like string matching and the likes, but it should serve to suffice. If I were doing this, I would go for a much more decoupled approach - you have started some of the way towards this, but there's still a long way to go to get it DI and MVVM.

          Chill _Maxxx_
          CodeStash - Online Snippet Management | My blog | MoXAML PowerToys | Mole 2010 - debugging made easier

          L 1 Reply Last reply
          0
          • P Pete OHanlon

            The tabs are there - all you need to do is search through your ObservableCollection of tabs and see if there's one of the type you want to create. That's what I was alluding to. A naive implementation would be something like this:

            if (viewType == "Users")
            {
            foreach (var tab in ObservableCollectionTabItems)
            {
            if (tab is VMUserExplorer)
            return;
            }
            // The rest of your processing occurs here.
            }

            You could simplify this somewhat so that your AddTab and constructor looks like this :

            private Dictionary viewsDictionary = new Dictionary();
            public VMBackOfficeWindow()
            {
            ObservableCollectionTabItems = new ObservableCollection();
            viewsDictionary.Add("Users", new VMUserExplorer());
            viewsDictionary.Add("Category Explorer", new VMCategoryExplorer());
            }
            public void AddTab(string viewType)
            {
            VMParentForViews vm = viewsDictionary[viewType];
            Type vmType = vm.GetType();
            foreach (var tab in ObservableCollectionTabItems)
            {
            if (tab.GetType() == vmType)
            return;
            }
            ObservableCollectionTabItems.Add(vm);
            // Carry on processing here.
            }

            Again, it's still a relatively naive solution, and relies an awful lot on things like string matching and the likes, but it should serve to suffice. If I were doing this, I would go for a much more decoupled approach - you have started some of the way towards this, but there's still a long way to go to get it DI and MVVM.

            Chill _Maxxx_
            CodeStash - Online Snippet Management | My blog | MoXAML PowerToys | Mole 2010 - debugging made easier

            L Offline
            L Offline
            LAPEC
            wrote on last edited by
            #5

            Thank you very much Pete that solved my problem... Once again thank you kind regards roni

            P 1 Reply Last reply
            0
            • L LAPEC

              Thank you very much Pete that solved my problem... Once again thank you kind regards roni

              P Offline
              P Offline
              Pete OHanlon
              wrote on last edited by
              #6

              Not a problem. Glad I can be of service.

              Chill _Maxxx_
              CodeStash - Online Snippet Management | My blog | MoXAML PowerToys | Mole 2010 - debugging made easier

              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