私は数十の小さなツールのコレクションであるWPFプロジェクトを持っています。各ツールをメインのタブコントロールのタブアイテムに置き、各タブアイテムの部分的なMainWinowクラスを作成しました。しかし、ツール間の関係はほとんどないので、互いに干渉しないように各ツールをシールすることをお勧めします。それに、私は部分クラスが悪であると聞いた。ここの問題は、MainWindow以外のクラスは、私の知識によるとUI項目との通信が非常に難しいことです。私はどこに行くべきなのか?部分クラスよりもタブ項目とのやりとりの方が良い
ありがとうございます。
Blamのリクエストに応じて、私の現在のコードをもっと簡略化したものです。元のコードは大きすぎてここに貼り付けることはできません。
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>
部分クラス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, ...
*/
}
}
パーシャルクラス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
*/
}
}
部分クラス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
*/
}
}
部分クラスに分割する目的は、それらを異なるファイルに配置できることです。
私はいくつかのコードを投稿できますか? – Paparazzi
クラスが1つのタブアイテムが世界であり、それがその単一のタブアイテムに対応するMainWindowのように振る舞うようにする方法はありますか? – Bob
私は各ツールを独自のusercontrolにしています。 – mdm20