Click here to Skip to main content
15,888,733 members
Home / Discussions / C#
   

C#

 
QuestionPrintPreviewControl and HTML Pin
dSolariuM10-Jun-11 23:36
dSolariuM10-Jun-11 23:36 
AnswerRe: PrintPreviewControl and HTML Pin
Luc Pattyn11-Jun-11 0:11
sitebuilderLuc Pattyn11-Jun-11 0:11 
GeneralRe: PrintPreviewControl and HTML Pin
MumbleB11-Jun-11 1:19
MumbleB11-Jun-11 1:19 
GeneralRe: PrintPreviewControl and HTML Pin
dSolariuM12-Jun-11 2:32
dSolariuM12-Jun-11 2:32 
GeneralRe: PrintPreviewControl and HTML Pin
dSolariuM12-Jun-11 2:33
dSolariuM12-Jun-11 2:33 
AnswerRe: PrintPreviewControl and HTML Pin
Luc Pattyn12-Jun-11 2:55
sitebuilderLuc Pattyn12-Jun-11 2:55 
GeneralRe: PrintPreviewControl and HTML Pin
BobJanova12-Jun-11 22:11
BobJanova12-Jun-11 22:11 
QuestionSuggestions On How To Refactor This Pin
Kevin Marois10-Jun-11 14:02
professionalKevin Marois10-Jun-11 14:02 

I'm working on a WPF app using MVVM with Telerik controls. I have a Telerik RadTreeView loaded with clients and projects:

<telerik:RadTreeView Width="200" 
                     HorizontalAlignment="Left"
                     ItemsSource="{Binding Path=Clients}"
                     SelectedItem="{Binding SelectedItem, Mode=TwoWay}"/>


Here are the 2 data models:
http://www.maroisconsulting.com/stuff/models.png[^]

When a node is clicked, the SelectedItem's Get calls nodeSelected. Here's where the fun begins.

The nodeSelected methods is responsible for
1) Extracting the DataModel (either the ClientModel or ProjectModel) from the selected node
2) Creating the view (ClientView or ProjectView)
3) Creating the viewmodel (ClientViewModel or ProjectViewModel)
4) Adding a tab to a RadTabControl and assigning the view to it.

While this all works fine, the code doesn't feel right. In the code below, you'll see that both the Client and Project sections have alot in common. I'd like to get some suggestions on refactoring it (Sorry it's a bit long):

private void nodeSelected()
{
    if (SelectedItem == null)
        return;

    string selectedObjectName = SelectedItem.GetType().Name.Trim().ToLower();

    switch (selectedObjectName)
    {
        #region Client
        case "clientmodel":

            // Get the client model from the selected tree node
            ClientModel clientModel = SelectedItem as ClientModel;

            // Check to see if a view with the client's Id is already open in a tab
            TabInfo clientTabInfo = getTabInfo(AppViews.Client, clientModel.ClientId);

            if (clientTabInfo == null)
            {
                string clientName = "Client - " + clientModel.ClientName;
                string clientImage = "/Falcon;component/Media/Graphics/customer_48x48.png";
                string clientIcon = "/Falcon;component/Media/Graphics/customer_16x16.png";

                // Create the client view model, passing in the client model
                ClientViewModel clientViewModel = new ClientViewModel(clientModel)
                {
                    HeaderTitle = clientName,
                    HeaderImage = clientImage
                };

                // Create the client view
                ClientView clientView = new ClientView
                { 
                    /* Assign the client view model to its view */
                    DataContext = clientViewModel };

                // Create the tab info object and add it to the list
                TabInfo info = new TabInfo
                {
                    ItemId = clientModel.ClientId,
                    ViewType = AppViews.Client
                };

                // Assign the Client's view and the tab info object to the tab
                // Create the new tab
                RadTabItem tab = addTab(clientName, clientIcon);
                tab.Content = clientView;
                tab.Tag = info;

                // Add the tab to the Tabs collection and select it
                Tabs.Add(tab);
                SelectedTab = tab;
            }
            else
            {
                // Actvate the tab
                activateTab(AppViews.Client, clientModel.ClientId);
            }
            break;

        #endregion

        #region Project
        case "projectmodel":

            // Get the client model from the selected tree node
            ProjectModel ProjectModel = SelectedItem as ProjectModel;

            // Check to see if a view with the Project's Id is already open in a tab
            TabInfo projectTabInfo = getTabInfo(AppViews.Project, ProjectModel.ProjectId);

            if (projectTabInfo == null)
            {
                string projectName = "Project - " + ProjectModel.Caption;
                string projectImage = "/Falcon;component/Media/Graphics/project_48x48.png";
                string projectIcon = "/Falcon;component/Media/Graphics/project_16x16.png";

                // Create the Project view model, passing in the Project model
                ProjectViewModel ProjectViewModel = new ProjectViewModel(ProjectModel)
                {
                    HeaderTitle = projectName,
                    HeaderImage = projectImage
                };

                // Create the Project view
                ProjectView ProjectView = new ProjectView();

                // Assign the Project view model to its view
                ProjectView.DataContext = ProjectViewModel;

                // Create the tab info object and add it to the list
                TabInfo info = new TabInfo
                {
                    ItemId = ProjectModel.ProjectId,
                    ViewType = AppViews.Project
                };

                // Create the new tab
                RadTabItem tab = addTab(projectName, projectIcon);
                tab.Content = ProjectView;
                tab.Tag = info;

                // Add the tab to the Tabs collection and select it
                Tabs.Add(tab);
                //SelectedTabIndex = Tabs.Count - 1;
                SelectedTab = tab;
            }
            else
            {
                // Actvate the tab
                activateTab(AppViews.Project, ProjectModel.ProjectId);
            }
            break;
        #endregion
    }

}

Everything makes sense in someone's mind

AnswerRe: Suggestions On How To Refactor This Pin
BobJanova12-Jun-11 23:44
BobJanova12-Jun-11 23:44 
QuestionSaving changed data from a BindingSource Pin
tekturtle10-Jun-11 4:26
tekturtle10-Jun-11 4:26 
AnswerRe: Saving changed data from a BindingSource Pin
BobJanova10-Jun-11 6:05
BobJanova10-Jun-11 6:05 
GeneralRe: Saving changed data from a BindingSource Pin
tekturtle10-Jun-11 10:04
tekturtle10-Jun-11 10:04 
GeneralRe: Saving changed data from a BindingSource Pin
BobJanova12-Jun-11 22:14
BobJanova12-Jun-11 22:14 
GeneralRe: Saving changed data from a BindingSource Pin
tekturtle15-Jun-11 11:57
tekturtle15-Jun-11 11:57 
GeneralRe: Saving changed data from a BindingSource [modified] Pin
BobJanova15-Jun-11 13:20
BobJanova15-Jun-11 13:20 
Questionsetup - include driver installation Pin
lukeer10-Jun-11 2:10
lukeer10-Jun-11 2:10 
AnswerRe: setup - include driver installation Pin
Dave Kreskowiak10-Jun-11 3:05
mveDave Kreskowiak10-Jun-11 3:05 
GeneralRe: setup - include driver installation Pin
lukeer10-Jun-11 3:31
lukeer10-Jun-11 3:31 
GeneralRe: setup - include driver installation Pin
Dave Kreskowiak10-Jun-11 3:53
mveDave Kreskowiak10-Jun-11 3:53 
QuestionList Of DataRow Pin
om_metab9-Jun-11 22:50
om_metab9-Jun-11 22:50 
AnswerRe: List Of DataRow Pin
V.9-Jun-11 22:57
professionalV.9-Jun-11 22:57 
AnswerRe: List Of DataRow Pin
BobJanova9-Jun-11 23:47
BobJanova9-Jun-11 23:47 
AnswerRe: List Of DataRow Pin
Pete O'Hanlon10-Jun-11 0:13
mvePete O'Hanlon10-Jun-11 0:13 
GeneralRe: List Of DataRow Pin
om_metab10-Jun-11 0:35
om_metab10-Jun-11 0:35 
QuestionDetecting USB devices Pin
majamer9-Jun-11 21:46
majamer9-Jun-11 21:46 

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.