次の方法で共有


プロパティ グリッドの拡張性

デザイナー内で特定のアクティビティが選択されたときに表示されるプロパティ グリッドをカスタマイズして、豊富な編集エクスペリエンスを作成できます。 PropertyGridExtensibility サンプルは、これを行う方法を示しています。

対象

ワークフロー設計ツールのプロパティグリッド拡張性。

議論

プロパティ グリッドを拡張するために、開発者には、プロパティ グリッド エディターのインラインの外観をカスタマイズしたり、より高度な編集画面用に表示されるダイアログを提供したりするオプションがあります。 このサンプルでは、2 つの異なるエディターが示されています。インライン エディターとダイアログ エディター。

インライン エディター

インライン エディターのサンプルでは、次の例を示します。

  • PropertyValueEditorから派生する型を作成します。

  • コンストラクターでは、 InlineEditorTemplate 値は Windows Presentation Foundation (WPF) データ テンプレートで設定されます。 これは XAML テンプレートにバインドできますが、このサンプルでは、コードを使用してデータ バインディングを初期化します。

  • データ テンプレートには、プロパティ グリッドに表示される項目の PropertyValue のデータ コンテキストがあります。 次のコード (CustomInlineEditor.cs) では、このコンテキストが Value プロパティにバインドされることに注意してください。

    FrameworkElementFactory stack = new FrameworkElementFactory(typeof(StackPanel));
    FrameworkElementFactory slider = new FrameworkElementFactory(typeof(Slider));
    Binding sliderBinding = new Binding("Value");
    sliderBinding.Mode = BindingMode.TwoWay;
    slider.SetValue(Slider.MinimumProperty, 0.0);
    slider.SetValue(Slider.MaximumProperty, 100.0);
    slider.SetValue(Slider.ValueProperty, sliderBinding);
    stack.AppendChild(slider);
    
  • アクティビティとデザイナーは同じアセンブリ内にあるため、アクティビティ デザイナー属性の登録は、SimpleCodeActivity.csの次の例に示すように、アクティビティ自体の静的コンストラクターで行われます。

    static SimpleCodeActivity()
    {
        AttributeTableBuilder builder = new AttributeTableBuilder();
        builder.AddCustomAttributes(typeof(SimpleCodeActivity), "RepeatCount", new EditorAttribute(typeof(CustomInlineEditor), typeof(PropertyValueEditor)));
        builder.AddCustomAttributes(typeof(SimpleCodeActivity), "FileName", new EditorAttribute(typeof(FilePickerEditor), typeof(DialogPropertyValueEditor)));
        MetadataStore.AddAttributeTable(builder.CreateTable());
    }
    

ダイアログ エディター

ダイアログ エディターのサンプルでは、次の例を示します。

  1. DialogPropertyValueEditorから派生する型を作成します。

  2. WPF データ テンプレートを使用してコンストラクターの InlineEditorTemplate 値を設定します。 これは XAML で作成できますが、このサンプルではコードで作成されます。

  3. データ テンプレートには、プロパティ グリッドに表示される項目の PropertyValue のデータ コンテキストがあります。 次のコードでは、これは Value プロパティにバインドされます。 また、FilePickerEditor.csでダイアログを発生させるボタンを提供する EditModeSwitchButton も含める必要があります。

    this.InlineEditorTemplate = new DataTemplate();
    
    FrameworkElementFactory stack = new FrameworkElementFactory(typeof(StackPanel));
    stack.SetValue(StackPanel.OrientationProperty, Orientation.Horizontal);
    FrameworkElementFactory label = new FrameworkElementFactory(typeof(Label));
    Binding labelBinding = new Binding("Value");
    label.SetValue(Label.ContentProperty, labelBinding);
    label.SetValue(Label.MaxWidthProperty, 90.0);
    
    stack.AppendChild(label);
    
    FrameworkElementFactory editModeSwitch = new FrameworkElementFactory(typeof(EditModeSwitchButton));
    
    editModeSwitch.SetValue(EditModeSwitchButton.TargetEditModeProperty, PropertyContainerEditMode.Dialog);
    
    stack.AppendChild(editModeSwitch);
    
    this.InlineEditorTemplate.VisualTree = stack;
    
  4. デザイナーの種類の ShowDialog メソッドをオーバーライドして、ダイアログの表示を処理します。 このサンプルでは、基本的な FileDialog を示します。

    public override void ShowDialog(PropertyValue propertyValue, IInputElement commandSource)
    {
        Microsoft.Win32.OpenFileDialog ofd = new Microsoft.Win32.OpenFileDialog();
        if (ofd.ShowDialog() == true)
        {
            propertyValue.Value = ofd.FileName;
        }
    }
    
  5. アクティビティとデザイナーは同じアセンブリ内にあるため、アクティビティ デザイナー属性の登録は、SimpleCodeActivity.csの次の例に示すように、アクティビティ自体の静的コンストラクターで行われます。

    static SimpleCodeActivity()
    {
        AttributeTableBuilder builder = new AttributeTableBuilder();
        builder.AddCustomAttributes(typeof(SimpleCodeActivity), "RepeatCount", new EditorAttribute(typeof(CustomInlineEditor), typeof(PropertyValueEditor)));
        builder.AddCustomAttributes(typeof(SimpleCodeActivity), "FileName", new EditorAttribute(typeof(FilePickerEditor), typeof(DialogPropertyValueEditor)));
        MetadataStore.AddAttributeTable(builder.CreateTable());
    }
    

サンプルを設定、ビルド、実行するには

  1. ソリューションをビルドし、Workflow1.xaml を開きます。

  2. ツールボックスからデザイナー キャンバスに SimpleCodeActivity をドラッグします。

  3. SimpleCodeActivity をクリックし、スライダー コントロールとファイル 選択コントロールがあるプロパティ グリッドを開きます。