0

I have a WPF project which is simply a collection of tens of small tools. I put each tool on a tabitem of a main tabcontrol and wrote a partial MainWinow class for each tabitem. However, since there is very little relationship between the tools, I would prefer to seal each tool so that they won't interfere with each other. Besides, I heard that partial class is evil. The problem here is a class other than MainWindow is very difficult to communicate with UI items (according to my knowledge). Any suggestions of where I should go?

Thanks a lot.

Upon Blam's request, here is a much simplified version of my current code. The original code is too large to paste here.

xaml:

<Window x:Class="WpfApplication7.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="MainWindow" Height="350" Width="525">
<Grid>
    <TabControl Margin="0,0,0,0" Name="tabControl1">
        <TabItem Header="Tool1" Name="Tool1">
            <Grid>
                <Label Name="lblTool1"/>
            </Grid>
        </TabItem>
        <TabItem Header="Tool2" Name="Tool2">
            <Grid>
                <Label Name="lblTool2"/>
            </Grid>
        </TabItem>
    </TabControl>
</Grid>
</Window>

Partial class 1 (MainWindow.xaml.cs):

namespace WpfApplication7
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        string caption = "Tool Collection";
        public MainWindow()
        {
            InitializeComponent();
            this.Title = caption;

            InitialTool1();
            InitialTool2();

        }
        /*
            Some other methods for main window, including those for menu bar, tool bar, ... 
        */
    }
}

Partial class 2 (Tool1.cs):

namespace WpfApplication7
{
    string tool1Details = "This is tool 1";
    /* 
        Other parameters related to tool1 
    */
    public partial class MainWindow : Window
    {

        public void InitialTool1()
        {
            lblTool1.Content = tool1Details;

        }
        /*
            Some other methods that communicate with tabitem1
        */
    }
}

Partial Class 3 (Tool2.cs):

namespace WpfApplication7
{
    public partial class MainWindow : Window
    {
        string tool2Detail = "This is tool 2";
        /* 
             Other parameters related to tool2 
        */
        public void InitialTool2()
        {
            lblTool2.Content = tool2Detail;

        }
        /*
            Some other methods that communicate with tabitem2
         */

    }
}

The purpose of split them into partial class is that we can put them in different files.

Bob
  • 381
  • 4
  • 14
  • I sure don't follow can you post some code? – paparazzo May 09 '12 at 20:12
  • Is there a way to make a class believes that one single tabitem is the world and make it behaves like the MainWindow corresponding to that single tabitem? – Bob May 09 '12 at 22:01
  • I'd look into making each tool it's own usercontrol. – mdm20 May 09 '12 at 22:04
  • Could you give any more details? Do you mean that I should create a control for each tool and than add them onto the big tabcontrol? – Bob May 10 '12 at 12:29
  • Thanks, mdm20. I will adopt your way. However, I have no way to set yours as the answer since you are using comments. I am sorry for that. – Bob May 10 '12 at 19:12

1 Answers1

0

Why not just pull

    public void InitialTool2()
    {
        lblTool2.Content = "This is tool 2";
        /*
           Lot of codes that communicate with tabitem2
         */
    }

And all other InitializeX into the first Partial class. The are separated into methods and yet can communicate with them?

paparazzo
  • 44,497
  • 23
  • 105
  • 176
  • There is not only one method related to Tool2/Tool1 since my code is real time updating. The main file would be very huge and confusing to put all of the methods together. I split them into each partial class so that I can put different methods on different files. I am sorry it is not clearly shown in the example. I will update the example to illustrate this idea. Thanks. – Bob May 10 '12 at 12:10
  • OK but as you said you said a communication problem. I use Regions to organize code. – paparazzo May 10 '12 at 15:34
  • The main problem with region or partial class is that the fields/method are visible to each other. When working on a new tab, I need to check if a field/method I am defining has been defined in other tabs. This can be very complex when there are a lot tools. – Bob May 10 '12 at 18:34
  • Here is an example, if I defined a field: "name" in tool1, then in "tool2", I cannot define a field with the same "name" in the same level. What's even worse, if I forgot to define the new variable and directly use it in tool2, it will have a value. If I make changes to it, I will change the variable in tool1. I will need something to say that the first "name" belongs to tool1, while the second "name" belongs to tool2. – Bob May 10 '12 at 18:35
  • Then we don't you do what mdm20 suggested 20 hours ago and make each tool it's own usercontrol? Show some effort. – paparazzo May 10 '12 at 18:44
  • Probably that is the best approach. I was expecting some less changes though. I am probably too lazy. :) – Bob May 10 '12 at 19:08