簡體   English   中英

可以調用 WPF 類型編輯器的構造函數(從 ITypeEditor 繼承)?

[英]Possible to call the constructor of a WPF type editor (inheriting from ITypeEditor)?

在回答這個問題時,建議我使用依賴注入來為從ITypeEditor繼承的類型編輯器提供自定義(通過“[將某些內容] 傳遞到 [類型編輯器] 的構造函數中”

由於這個類型編輯器只是用來修飾適當的參數,我不知道該怎么做。 我沒有顯式地實例化它,所以我沒有調用它的構造函數。

盡管我找到了與類似情況有關的聲明(盡管使用UITypeEditor ),但我在網上找不到解決方案/解釋, 據說“由於您沒有創建自己的UITypeEditor衍生的實例,因此您沒有控制將 arguments 傳遞給構造函數的內容” 這與我的想法一致。

那么,我可以訪問構造函數以將參數傳遞給它嗎?如果可以,如何?

[Editor(typeof(MyControls.PropertyGridFilePicker), typeof(MyControls.PropertyGridFilePicker))]
public string ProjectFolder { get; set; } = "";

public partial class PropertyGridFilePicker : ITypeEditor
{
    
    public PropertyGridFilePicker()
    {
        InitializeComponent();

        // Constructor - if I could pass parameters into this it would of course be very useful

    }

    // The rest of the type editor methods go here

}

在您的上下文中,依賴注入並不能解決問題。 正如您所注意到的,您無法控制類型的實例創建。 為了允許依賴注入,API 必須支持它,例如,通過接受抽象工廠作為參數。

為了解決您的問題,您應該創建一個抽象基類型來封裝所有編輯器的常見行為。 與添加配置參數來控制/擴展行為相比,使用特化也更易於維護。 鑒於您發布的代碼,我假設您想在按下瀏覽按鈕時執行專門的操作。 您應該通過在您的基類型中將此行為實現為抽象來將此行為委托給子類。 然后只需使用特殊類的類型作為EditorAttribute的參數:

PropertyGridBrowser.cs

// The abstract common base class. Override 'ExecuteBrowse' to extend behavior.
public abstract class PropertyGridBrowser : UserControl, ITypeEditor
{
    public string Value
    {
        get { return (string)GetValue(ValueProperty); }
        set { SetValue(ValueProperty, value); }
    }

    // Using a DependencyProperty as the backing store for Value.  This enables animation, styling, binding, etc...
    public static readonly DependencyProperty ValueProperty =
        DependencyProperty.Register("Value", typeof(string), typeof(PropertyGridFilePicker), new PropertyMetadata(null));

    protected PropertyGridBrowser()
    {
        InitializeComponent();
    }   

    public FrameworkElement ResolveEditor(PropertyItem propertyItem)
    {
        Binding binding = new Binding("Value");
        binding.Source = propertyItem;
        binding.Mode = propertyItem.IsReadOnly ? BindingMode.OneWay : BindingMode.TwoWay;
        BindingOperations.SetBinding(this, ValueProperty, binding);
        return this;
    }

    protected abstract void ExecuteBrowse();

    private void PickFileButton_Click(object sender, RoutedEventArgs e)
    {
        ExecuteBrowse();
    }
}

PropertyGridFilePicker.cs

public partial class PropertyGridFilePicker : PropertyGridBrowser
{
    string rtn = "";
    public PropertyGridFilePicker() : base()
    {
    }

    protected override void ExecuteBrowse()
    {
        OpenFileDialog fd = new OpenFileDialog();
        if (fd.ShowDialog() == true && fd.CheckFileExists)
        {
            Value = fd.FileName;
        }
    }
}

PropertyGridFolderPicker.cs

public partial class PropertyGridFolderPicker : PropertyGridBrowser
{
    public PropertyGridFilePicker() : base()
    {
    }

    protected override void ExecuteBrowse()
    {
        var dialog = new System.Windows.Forms.FolderBrowserDialog();
        System.Windows.Forms.DialogResult result = dialog.ShowDialog();

        if (result == System.Windows.Forms.DialogResult.OK)
        {
            
        }
    }
}

例子

// Use a file browser
[Editor(typeof(MyControls.PropertyGridFilePicker), typeof(MyControls.PropertyGridFilePicker))]
public string ProjectFolder { get; set; } = "";

// Use a folder browser
[Editor(typeof(MyControls.PropertyGridFolderPicker), typeof(MyControls.PropertyGridFolderPicker))]
public string ProjectFolder { get; set; } = "";

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM