次のコード例は、WPF Designer for Visual Studio 用のカスタム ダイアログ ボックス プロパティ値エディターを実装する方法を示しています。完全なソリューションについては、「WPF Designer Extensibility Samples (WPF デザイナーの機能拡張のサンプル)」のサイトに掲載されているダイアログ プロパティ値エディターのサンプルを参照してください。
使用例
このトピックでは、[プロパティ] ウィンドウでカスタム FileName プロパティがクリックされたときに [ファイルを開く] ダイアログ ボックスを表示する、ダイアログ ボックス プロパティ値エディターを作成する方法について説明します。
using System;
using System.ComponentModel;
using System.Windows.Controls;
using System.Windows;
namespace CustomControlLibrary
{
public partial class DemoControl : UserControl
{
public DemoControl()
{
InitializeComponent();
}
public static readonly DependencyProperty FileNameProperty = DependencyProperty.Register(
"FileName",
typeof(string),
typeof(DemoControl),
new PropertyMetadata("File name not set."));
public string FileName
{
get
{
return (string)this.GetValue(FileNameProperty);
}
set
{
this.SetValue(FileNameProperty, value);
}
}
}
}
<ResourceDictionary xmlns="https://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="https://schemas.microsoft.com/winfx/2006/xaml"
xmlns:PropertyEditing="clr-namespace:Microsoft.Windows.Design.PropertyEditing;assembly=Microsoft.Windows.Design.Interaction"
xmlns:Local="clr-namespace:CustomControlLibrary.Design"
x:Class="CustomControlLibrary.Design.EditorResources">
<DataTemplate x:Key="FileBrowserInlineEditorTemplate">
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="1*"/>
<ColumnDefinition Width="Auto"/>
</Grid.ColumnDefinitions>
<TextBox Grid.Column="0" Text="{Binding StringValue}"/>
<PropertyEditing:EditModeSwitchButton Grid.Column="1"/>
</Grid>
</DataTemplate>
</ResourceDictionary>
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace CustomControlLibrary.Design
{
using System.Windows;
public partial class EditorResources : ResourceDictionary
{
public EditorResources()
: base()
{
InitializeComponent();
}
}
}
using System;
using System.ComponentModel;
using System.Windows;
using Microsoft.Windows.Design.Metadata;
using Microsoft.Windows.Design.PropertyEditing;
using Microsoft.Win32;
namespace CustomControlLibrary.Design
{
public class FileBrowserDialogPropertyValueEditor : DialogPropertyValueEditor
{
private EditorResources res = new EditorResources();
public FileBrowserDialogPropertyValueEditor()
{
this.InlineEditorTemplate = res["FileBrowserInlineEditorTemplate"] as DataTemplate;
}
public override void ShowDialog(
PropertyValue propertyValue,
IInputElement commandSource)
{
OpenFileDialog ofd = new OpenFileDialog();
ofd.Multiselect = false;
if (ofd.ShowDialog() == true)
{
propertyValue.StringValue = ofd.FileName;
}
}
}
}
using System;
using System.ComponentModel;
using System.Windows;
using Microsoft.Windows.Design.Metadata;
using Microsoft.Windows.Design.PropertyEditing;
// The ProvideMetadata assembly-level attribute indicates to designers
// that this assembly contains a class that provides an attribute table.
[assembly: ProvideMetadata(typeof(CustomControlLibrary.Design.Metadata))]
namespace CustomControlLibrary.Design
{
// Container for any general design-time metadata to initialize.
// Designers look for a type in the design-time assembly that
// implements IProvideAttributeTable. If found, designers instantiate
// this class and access its AttributeTable property automatically.
internal class Metadata : IProvideAttributeTable
{
// Accessed by the designer to register any design-time metadata.
public AttributeTable AttributeTable
{
get
{
AttributeTableBuilder builder = new AttributeTableBuilder();
builder.AddCustomAttributes
(typeof(CustomControlLibrary.DemoControl),
"FileName",
PropertyValueEditor.CreateEditorAttribute(
typeof(FileBrowserDialogPropertyValueEditor)));
return builder.CreateTable();
}
}
}
}
<Window x:Class="WpfApplication1.MainWindow"
xmlns="https://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="https://schemas.microsoft.com/winfx/2006/xaml"
xmlns:ccl="clr-namespace:CustomControlLibrary;assembly=CustomControlLibrary"
Title="MainWindow" Height="300" Width="300">
<Grid>
<ccl:DemoControl FileName="" />
</Grid>
</Window>
コードのコンパイル
3 つのアセンブリで、前のコード例をコンパイルします。
カスタム コントロールのコンパイル
Visual Studio で、CustomControlLibrary という名前の新しい WPF ユーザー コントロール ライブラリ プロジェクトを C# で作成します。
"UserControl1" の出現箇所をすべて "DemoControl" に置き換えます。
DemoControl クラス内の既存のコードを、前に示したコードに置き換えます。
ソリューションをビルドします。
カスタム ダイアログ ボックス プロパティ値エディターのコンパイル
Visual Studio で、CustomControlLibrary.Design という名前の新しい WPF ユーザー コントロール ライブラリ プロジェクトをソリューションに追加します。
プロジェクトの出力パスを ".. \CustomControlLibrary\bin\Debug\" に設定します。
UserControl1.xaml および UserControl1.xaml.cs をプロジェクトから削除します。
次のアセンブリへの参照を追加します。
Microsoft.Windows.Design.Extensibility
Microsoft.Windows.Design.Interaction
CustomControlLibrary プロジェクトへの参照を追加します。
EditorResources という名前のリソース ディクショナリをプロジェクトに追加します。
EditorResources.xaml 内の既存の XAML を、前に示した XAML に置き換えます。
EditorResources という名前の新しいクラスをプロジェクトに追加します。
EditorResources 内の既存のコードを、前に示したコードに置き換えます。
FileBrowserDialogPropertyValueEditor という名前の新しいクラスをプロジェクトに追加します。
FileBrowserDialogPropertyValueEditor クラス内の既存のコードを、前に示したコードに置き換えます。
Metadata という名前の新しいクラスをプロジェクトに追加します。
Metadata クラス内の既存のコードを、前に示したコードに置き換えます。
ソリューションをビルドします。
テスト アプリケーションのコンパイル
Visual Studio で、新しい WPF アプリケーション プロジェクトをソリューションに追加します。
CustomControlLibrary アセンブリまたはプロジェクトへの参照を追加します。
MainWindow.xaml の XAML ビューで、既存の XAML を前の XAML に置き換えます。
MainWindow.xaml.cs で、InitializeComponent の呼び出しをコメント アウトします。
ソリューションをビルドし直します。
デザイン ビューで、DemoControl をクリックしてこれを選択します。 必要に応じて、デザイナーの最上部の情報バーをクリックしてビューを再度読み込みます。
[プロパティ] ウィンドウで、FileName プロパティの横にあるボタンをクリックします。
[開く] ダイアログ ボックスが表示されます。
ファイルを選択して、[開く] をクリックします。
[プロパティ] ウィンドウの FileName プロパティにファイル名が表示され、XAML ビュー内に FileName プロパティが割り当てられます。