다음을 통해 공유


방법: 대화 상자 속성 값 편집기 만들기

다음 코드 예제에서는 WPF Designer for Visual Studio에 대한 사용자 지정 대화 상자 속성 값 편집기를 구현하는 방법을 보여 줍니다. 전체 솔루션은 WPF and Silverlight Designer Extensibility Samples 사이트의 Dialog Property Value Editor 샘플을 참조하십시오. 

예제

이 항목에서는 속성 창에서 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>

코드 컴파일

이전 예제 코드를 서로 다른 세 어셈블리로 컴파일합니다.

사용자 지정 컨트롤 컴파일

  1. Visual Studio에서 C#으로 CustomControlLibrary라는 새 WPF 사용자 정의 컨트롤 라이브러리 프로젝트를 만듭니다.

  2. 모든 "UserControl1"을 "DemoControl"로 변경합니다.

  3. DemoControl 클래스의 기존 코드를 앞에 나오는 코드로 바꿉니다.

  4. 솔루션을 빌드합니다.

사용자 지정 대화 상자 속성 값 편집기 컴파일

  1. Visual Studio에서 CustomControlLibrary.Design이라는 새 WPF 사용자 정의 컨트롤 라이브러리 프로젝트를 솔루션에 추가합니다.

  2. 프로젝트의 출력 경로를 ".. \CustomControlLibrary\bin\Debug\"로 설정합니다.

  3. 프로젝트에서 UserControl1.xaml 및 UserControl1.xaml.cs를 삭제합니다.

  4. 다음 어셈블리에 대한 참조를 추가합니다.

    • Microsoft.Windows.Design.Extensibility

    • Microsoft.Windows.Design.Interaction

  5. CustomControlLibrary 프로젝트에 대한 참조를 추가합니다.

  6. 프로젝트에 EditorResources라는 리소스 사전을 추가합니다.

  7. EditorResources.xaml의 기존 XAML을 앞에 나오는 XAML로 바꿉니다.

  8. EditorResources라는 새 클래스를 프로젝트에 추가합니다.

  9. EditorResources의 기존 코드를 앞에 나오는 코드로 바꿉니다.

  10. 프로젝트에 FileBrowserDialogPropertyValueEditor라는 새 클래스를 추가합니다.

  11. FileBrowserDialogPropertyValueEditor 클래스의 기존 코드를 앞에 나오는 코드로 바꿉니다.

  12. Metadata라는 새 클래스를 프로젝트에 추가합니다.

  13. Metadata 클래스의 기존 코드를 앞에 나오는 코드로 바꿉니다.

  14. 솔루션을 빌드합니다.

테스트 응용 프로그램 컴파일

  1. Visual Studio에서 솔루션에 새 WPF 응용 프로그램 프로젝트를 추가합니다.

  2. CustomControlLibrary 어셈블리 또는 프로젝트에 대한 참조를 추가합니다.

  3. MainWindow.xaml의 XAML 뷰에서 기존 XAML을 앞에 나오는 XAML로 바꿉니다.

  4. MainWindow.xaml.cs에서 InitializeComponent에 대한 호출을 주석으로 처리합니다.

  5. 솔루션을 다시 빌드합니다.

  6. 디자인 뷰에서 DemoControl을 클릭하여 선택합니다. 디자이너 위쪽의 정보 표시줄을 클릭하여 뷰를 다시 로드해야 할 수 있습니다.

  7. 속성 창에서 FileName 속성 옆에 있는 단추를 클릭합니다.

    열기 대화 상자가 나타납니다.

  8. 파일을 찾아 열기를 클릭합니다.

    속성 창의 FileName 속성에 파일 이름이 표시되고 XAML 뷰에서 FileName 속성이 지정됩니다.

참고 항목

참조

ItemPolicy

PrimarySelectionPolicy

기타 리소스

고급 확장성 개념

WPF Designer 확장성

WPF and Silverlight Designer Extensibility Samples