6.9 ツールバー
6.9.1 バーを表示する
多くのアプリケーションでは、頻繁に使われるメニュー項目にすばやくアクセスすることができるツールバーを備えています。通常、ツールバーはメニューバーの下部に配置され、必要に応じてユーザーがボタンやバーの位置を変更することも可能なアプリケーションもあります。
WPF では、メニューと同様にツールバーも UIElement から派生する要素であり、他のコントロールと同様に大きな制限を受けることなく自由に配置することができます。必要に応じて、ツールバー上に任意の図形やコントロールを表示することもできるのです。
ツールバーを表示するには System.Windows.Controls.ToolBar クラスを使います。
System.Object System.Windows.Threading.DispatcherObject System.Windows.DependencyObject System.Windows.Media.Visual System.Windows.UIElement System.Windows.FrameworkElement System.Windows.Controls.Control System.Windows.Controls.ItemsControl System.Windows.Controls.HeaderedItemsControl System.Windows.Controls.ToolBar
[TemplatePartAttribute(Name="PART_ToolBarPanel", Type=typeof(ToolBarPanel))] [TemplatePartAttribute(Name="PART_ToolBarOverflowPanel", Type=typeof(ToolBarOverflowPanel))] public class ToolBar : HeaderedItemsControl
このクラスのコンストラクタは、パラメータを受け取りません。
public ToolBar ()
ToolBar クラスは、ItemsControl クラスを継承しているので、バーの内部で管理するコントロールを任意の数だけ追加することができます。一般的なアプリケーションであれば、小さなアイコンイメージを表示するボタンをツールバーの項目として追加するでしょう。しかし、必要に応じてテキストボックスやコンボボックスを表示することも可能です。
ToolBar オブジェクトは、本来はメニューの下部に統合されますが、WPF では ToolBar も UIElement として自由に描画することができます。他の ContentControl オブジェクトの Content に設定したり、任意のパネルに貼り付けることができます。
using System; using System.Windows; using System.Windows.Controls; class Test { [STAThread] public static void Main() { Button button1 = new Button(); button1.Content = "●"; Button button2 = new Button(); button2.Content = "■"; ToolBar bar = new ToolBar(); bar.Items.Add(button1); bar.Items.Add(button2); Window wnd = new Window(); wnd.Content = bar; Application app = new Application(); app.Run(wnd); } }
コード1では、生成した ToolBar オブジェクトをウィンドウのコンテンツとして表示させています。ToolBar には 2 つのボタンを追加しているため、バー上にボタンが表示されています。これらのボタンは通常の Button オブジェクトなので、ボタンが押されたときのイベント処理などの方法は同じです。
6.9.2 複数のバーを管理する
コード1では、ウィンドウのコンテンツとしてツールバーを表示したため、ツールバーがウィンドウ全体に伸縮されて表示されました。WPF では、ツールバーでさえ、このように自由に描画することができてしまいますが、一般的なツールバーとして表示させたい場合は System.Windows.Controls.ToolBarTray クラスをコンテナとして利用します。
System.Object System.Windows.Threading.DispatcherObject System.Windows.DependencyObject System.Windows.Media.Visual System.Windows.UIElement System.Windows.FrameworkElement System.Windows.Controls.ToolBarTray
[ContentPropertyAttribute("ToolBars")] public class ToolBarTray : FrameworkElement, IAddChild
ToolBarTray クラスは、任意の数の ToolBar を表示するツールバーのコンテナとなるコントロールです。
このクラスのコンストラクタは、パラメータを受け取りません。
public ToolBarTray ()
ToolBarTray オブジェクトに表示させたい ToolBar オブジェクトを追加するには ToolBars プロパティから取得できるコレクションオブジェクトを利用します。
public Collection<ToolBar> ToolBars { get; }
ToolBars プロパティは、任意の数の ToolBar 型のオブジェクトを管理するコレクションオブジェクトを返します。このコレクションに ToolBar オブジェクトを追加することで、ToolBarTray コントロール上にツールバーを表示することができます。
using System; using System.Windows; using System.Windows.Controls; class Test { [STAThread] public static void Main() { Button button1 = new Button(); button1.Content = "●"; Button button2 = new Button(); button2.Content = "■"; Button button3 = new Button(); button3.Content = "▲"; ToolBar bar1 = new ToolBar(); bar1.Items.Add(button1); bar1.Items.Add(button2); ToolBar bar2 = new ToolBar(); bar2.Items.Add(button3); ToolBarTray tray = new ToolBarTray(); tray.ToolBars.Add(bar1); tray.ToolBars.Add(bar2); Window wnd = new Window(); wnd.Content = tray; Application app = new Application(); app.Run(wnd); } }
コード2は、2 つの ToolBar オブジェクトを ToolBarTray オブジェクトの ToolBars プロパティが返すコレクションに追加しています。ToolBarTray コントロールは、任意の数のツールバーを管理するコンテナの役割を果たします。実行結果のように、ToolBarTray は、保有する 2 つのツールバーを適切に表示しています。また、ToolBarTray に入れられたツールバーをドラッグすることで、自由に互いの位置を交換することができます。