Click here to Skip to main content
15,886,919 members
Home / Discussions / WPF
   

WPF

 
AnswerRe: My Thoughts On My WPF App Design Pin
Mycroft Holmes12-Jul-13 13:06
professionalMycroft Holmes12-Jul-13 13:06 
GeneralRe: My Thoughts On My WPF App Design Pin
Kevin Marois14-Jul-13 13:42
professionalKevin Marois14-Jul-13 13:42 
GeneralRe: My Thoughts On My WPF App Design Pin
Mycroft Holmes14-Jul-13 14:11
professionalMycroft Holmes14-Jul-13 14:11 
GeneralRe: My Thoughts On My WPF App Design Pin
Kevin Marois15-Jul-13 5:11
professionalKevin Marois15-Jul-13 5:11 
GeneralRe: My Thoughts On My WPF App Design Pin
Mycroft Holmes16-Jul-14 21:46
professionalMycroft Holmes16-Jul-14 21:46 
GeneralRe: My Thoughts On My WPF App Design Pin
Kevin Marois17-Jul-14 6:49
professionalKevin Marois17-Jul-14 6:49 
GeneralRe: My Thoughts On My WPF App Design Pin
Mycroft Holmes17-Jul-14 12:43
professionalMycroft Holmes17-Jul-14 12:43 
GeneralRe: My Thoughts On My WPF App Design Pin
Kevin Marois17-Jul-14 12:59
professionalKevin Marois17-Jul-14 12:59 
Ok, I see

My tab control is bound to a class, so in the XAML I have

<TabControl Grid.Row="2"
            Grid.Column="2"
            x:Name="MyTabControl"
            BorderBrush="#6593CF"
            Background="White"
            ItemsSource="{Binding TabManager.Tabs}"
            SelectedItem="{Binding TabManager.ActiveTab}"
            Margin="2"/>


My Tab Manager class is this

public class ViewManager : BindableBase, ITabManager
{
    #region Events
    public event EventHandler TabActivated;
    #endregion

    #region Bound Properties
    private ObservableCollection<TabItem> _Tabs = new ObservableCollection<TabItem>();
    public ObservableCollection<TabItem> Tabs
    {
        get { return _Tabs; }
        set
        {
            if (_Tabs != value)
            {
                _Tabs = value;
                RaisePropertyChanged("Tabs");
            }
        }
    }

    private TabItem _ActiveTab;
    public TabItem ActiveTab
    {
        get { return _ActiveTab; }
        set
        {
            if (_ActiveTab != value)
            {
                _ActiveTab = value;
                RaisePropertyChanged("ActiveTab");

                if (TabActivated != null)
                {
                    TabActivated(this, new EventArgs());
                }
            }
        }
    }

    private bool _TabsVisible;
    public bool TabsVisible
    {
        get { return _TabsVisible; }
        set
        {
            if (_TabsVisible != value)
            {
                _TabsVisible = value;
                RaisePropertyChanged("TabsVisible");
            }
        }
    }
    #endregion

    #region Public Methods
    public void ActivateTab(ViewInfo TabInfo)
    {
        var tab = GetTab(TabInfo);
        ActiveTab = tab;
    }
    public void AddTab(TabItem Tab, bool Activate = true, int IndexPosition = -1)
    {
        if (IndexPosition == -1)
        {
            Tabs.Add(Tab);
        }
        else
        {
            Tabs.Insert(IndexPosition, Tab);
        }

        if (Activate)
        {
            ActivateTab((ViewInfo)Tab.Tag);

            // Show/Hide the tab control based on whether there are any tabs open
            TabsVisible = Tabs.Count > 0;
        }
    }
    public bool CloseAllButThisTab(ViewInfo TabInfo)
    {
        bool retVal = true;

        for (int i = Tabs.Count - 1; i >=0; i-- )
        {
            var tab = Tabs[i];
            var tabInfo = (ViewInfo)tab.Tag;

            if (tabInfo.View == TabInfo.View && tabInfo.EntityId == TabInfo.EntityId)
            {
                continue;
            }
            else
            {
                retVal = CloseTab(tabInfo);

                if (!retVal)
                {
                    break;
                }
            }
        }

        return retVal;
    }
    public bool CloseAllTabs()
    {
        bool retVal = true;

        for(int i = Tabs.Count - 1; i > 0; i--)
        {
            var tab = Tabs[i];
            retVal = CloseTab((ViewInfo)tab.Tag);
        }

        return retVal;
    }
    public bool CloseTabAtIndex(int Index)
    {
        Tabs.RemoveAt(Index);
        return true;
    }
    public bool CloseActiveTab()
    {
        return CloseTab((ViewInfo)ActiveTab.Tag);
    }
    public bool CloseTab(ViewInfo TabInfo)
    {
        bool retVal = true;
        var tab = GetTab(TabInfo);

        if (tab != null)
        {
            // Make the tab active
            ActivateTab((ViewInfo)tab.Tag);

            // Get the view off the tab
            var view = (UserControl)tab.Content;

            // If the view is a Center...
            if (view.DataContext is _CenterViewModelBase)
            {
                // Get the view Entity off the center
                _CenterViewModelBase centerVM = (_CenterViewModelBase)view.DataContext;

                // Go through all the tab's subtabs and try to close each one...
                for(int i = centerVM.TabManager.Tabs.Count - 1; i > -1 ; i--)
                //foreach (var centerTab in centerVM.TabManager.Tabs)
                {
                    // Get the tab
                    TabItem centerTab = centerVM.TabManager.Tabs[i];

                    // Call back into this method, passing in the sub tab's View Info to close it
                    bool closed = centerVM.TabManager.CloseTab((ViewInfo)centerTab.Tag);

                    // If the tab was not closed...
                    if (!closed)
                    {
                        // Turn off the retun flag and exit...
                        retVal = false;
                        break;
                    }
                }
            }
            else
            {
                // Get the data entry VM off the tab
                dynamic vm = (_DataEntryViewModelBase)view.DataContext;

                // If the VM has pending changes, prompt the user to save
                retVal = vm.PromptSaveChanges(true);
            }

            // If ok to continue closing...
            if (retVal)
            {
                // If the tab was saved, remove it
                Tabs.Remove(tab);

                // Show/Hide the tab control based on whether there are any tabs open
                TabsVisible = Tabs.Count > 0;
            }
        }

        return retVal;
    }
    public bool HasDirtyTabs()
    {
        bool retVal = false;

        foreach(var tab in Tabs)
        {
            // Activate the tab
            ActivateTab((ViewInfo)tab.Tag);

            // Get the view off the tab
            var view = (UserControl)tab.Content;

            // Get the View Entity off the view
            _DataEntryViewModelBase vm = (_DataEntryViewModelBase)view.DataContext;

            // See if the tab is dirty
            retVal = vm.IsDirty();

            // If so, exit the loop
            if (retVal)
            {
                break;
            }
        }

        return retVal;
    }
    public bool IsTabOpen(ViewInfo TabInfo)
    {
        TabItem tabItem = GetTab(TabInfo);
        return tabItem != null;
    }
    public TabItem GetTab(ViewInfo TabInfo)
    {
        // This doesn't work for some reason
        //TabItem tab = Tabs.Where(t => ((ViewInfo)t.Tag).View == TabInfo.View && ((ViewInfo)t.Tag).EntityId == TabInfo.EntityId).FirstOrDefault();
        TabItem retVal = null;

        foreach (var tab in Tabs)
        {
            ViewInfo vi = (ViewInfo)tab.Tag;

            if (vi.View == TabInfo.View && vi.EntityId == TabInfo.EntityId)
            {
                retVal = tab;
                break;
            }

        }

        return retVal;
    }
    public void RefreshTabInfo(AppMessageArgs Args)
    {
        foreach (var tab in Tabs)
        {
            UserControl view = tab.Content as UserControl;
            _DataEntryViewModelBase vm = view.DataContext as _DataEntryViewModelBase;
            ViewInfo vi = (ViewInfo)tab.Tag;

            if (vi.View == Args.ViewType)
            {
                tab.Header = vm.GetEntityCaption(); ;
                vi.EntityId = vm.GetEntityId();

                tab.Tag = vi;
            }
        }
    }
    public bool SaveAllOpenTabs()
    {
        bool retVal = false;

        foreach (var tab in Tabs)
        {
            // Activate the tab
            ActivateTab((ViewInfo)tab.Tag);

            // Get the view off the tab
            var view = (UserControl)tab.Content;

            // Get the View Entity off the view
            _DataEntryViewModelBase vm = (_DataEntryViewModelBase)view.DataContext;

            // Attempt to save changes
            retVal = vm.PromptSaveChanges(true);

            // If not saved, cancel out
            if (!retVal)
            {
                break;
            }
        }

        return retVal;
    }
    #endregion
}


Then when I want to add a tab from a VM I do this:

private void loadView(BOMArgs Args)
{
    // Create a View Info instance
    ViewInfo tabInfo = new ViewInfo
    {
        View = View.BOM,
        EntityId = Args.BOM.Id
    };

    if (tabInfo.EntityId > 0 && TabManager.IsTabOpen(tabInfo))
    {
        TabManager.ActivateTab(tabInfo);
    }
    else
    {
        // Create an instance of the BOM view Entity and load it
        BOMViewModel vm = new BOMViewModel(Engine);
        vm.Load(Args);

        // Create the view and assign the view Entity to it
        UserControl view = new BOMView();
        view.DataContext = vm;

        // Cretae a tab and assign the presenter view to it
        TabItem tabItem = new TabItem
        {
            Header = vm.GetEntityCaption(),
            Content = view,
            Tag = tabInfo
        };

        // Add the tab and activate it
        TabManager.AddTab(tabItem, true);
    }
}


Works perfect, although I need to de-couple the view => vm dependency in the 'else' section.
If it's not broken, fix it until it is

GeneralRe: My Thoughts On My WPF App Design Pin
Mycroft Holmes17-Jul-14 14:09
professionalMycroft Holmes17-Jul-14 14:09 
QuestionWPF ComboBox Problem Pin
Kevin Marois10-Jul-13 19:13
professionalKevin Marois10-Jul-13 19:13 
AnswerRe: WPF ComboBox Problem Pin
Richard Deeming11-Jul-13 1:58
mveRichard Deeming11-Jul-13 1:58 
GeneralRe: WPF ComboBox Problem Pin
Kevin Marois11-Jul-13 17:48
professionalKevin Marois11-Jul-13 17:48 
GeneralRe: WPF ComboBox Problem Pin
Kevin Marois19-Jul-13 16:27
professionalKevin Marois19-Jul-13 16:27 
Questionhow to make an navigation title dropdownlist Pin
neodeaths10-Jul-13 9:45
neodeaths10-Jul-13 9:45 
AnswerRe: how to make an navigation title dropdownlist Pin
Mycroft Holmes10-Jul-13 12:44
professionalMycroft Holmes10-Jul-13 12:44 
QuestionAny Idea CFL in Silverlight? Pin
Tanmoy Mahish9-Jul-13 19:57
Tanmoy Mahish9-Jul-13 19:57 
AnswerRe: Any Idea CFL in Silverlight? Pin
Mycroft Holmes9-Jul-13 21:10
professionalMycroft Holmes9-Jul-13 21:10 
GeneralRe: Any Idea CFL in Silverlight? Pin
Tanmoy Mahish9-Jul-13 21:38
Tanmoy Mahish9-Jul-13 21:38 
GeneralRe: Any Idea CFL in Silverlight? Pin
Mycroft Holmes9-Jul-13 22:45
professionalMycroft Holmes9-Jul-13 22:45 
GeneralRe: Any Idea CFL in Silverlight? Pin
Tanmoy Mahish9-Jul-13 23:56
Tanmoy Mahish9-Jul-13 23:56 
GeneralRe: Any Idea CFL in Silverlight? Pin
Mycroft Holmes10-Jul-13 1:06
professionalMycroft Holmes10-Jul-13 1:06 
AnswerRe: Any Idea CFL in Silverlight? Pin
Richard MacCutchan10-Jul-13 1:40
mveRichard MacCutchan10-Jul-13 1:40 
Questionhow to use equalizer using naudio wpf Pin
thecco7-Jul-13 17:50
thecco7-Jul-13 17:50 
Question[SOLVED] WPF Printing in milimeters Pin
Saksida Bojan5-Jul-13 22:16
Saksida Bojan5-Jul-13 22:16 
SuggestionRe: WPF Printing in milimeters Pin
AlphaDeltaTheta7-Jul-13 21:18
AlphaDeltaTheta7-Jul-13 21:18 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.