How to avoid repeating dynamic tabitem at runtime?
-
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;
-
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;
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 -
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 -
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
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 -
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 -
Not a problem. Glad I can be of service.
Chill _Maxxx_
CodeStash - Online Snippet Management | My blog | MoXAML PowerToys | Mole 2010 - debugging made easier