簡體   English   中英

我可以創建一個接受XAML元素的DependencyProperty嗎?

[英]Can I create a DependencyProperty that accepts a XAML element?

我創建了一個名為BrowseButton的自定義類,該類擴展了Button 這個按鈕非常簡單; 單擊時,將彈出一個文件選擇器對話框。 我將其創建為自己的特殊類,因為我希望能夠在我的應用程序中快速輕松地重用它。 用戶成功選擇文件后,我還希望它使用完整的文件路徑在同一頁面上填充TextBox控件。

這是該按鈕的(C#)代碼:

using System;
using System.Windows;
using System.Windows.Controls;
using Microsoft.Win32;

namespace MyProject.Extensions
{
    public partial class BrowseButton : Button
    {
        public static readonly DependencyProperty DefaultExtDependency = DependencyProperty.Register("DefaultExt", typeof(string), typeof(BrowseButton));
        public static readonly DependencyProperty FilterDependency = DependencyProperty.Register("Filter", typeof(string), typeof(BrowseButton));
        public static readonly DependencyProperty TextBoxDependency = DependencyProperty.Register("TextBox", typeof(TextBox), typeof(BrowseButton));

        public string DefaultExt
        {
            get
            {
                return (string)GetValue(DefaultExtDependency);
            }
            set
            {
                SetValue(DefaultExtDependency, value);
            }
        }

        public string Filter
        {
            get
            {
                return (string)GetValue(FilterDependency);
            }
            set
            {
                SetValue(FilterDependency, value);
            }
        }

        public TextBox TextBox
        {
            get
            {
                return (TextBox)GetValue(TextBoxDependency);
            }
            set
            {
                SetValue(TextBoxDependency, value);
            }
        }

        public BrowseButton()
        {
            InitializeComponent();
        }

        public event EventHandler<string> FileSelected;

        public void Connect(int connectionId, object target)
        {

        }

        private void BrowseButton_OnClick(object sender, RoutedEventArgs e)
        {
            var dialog = new OpenFileDialog
            {
                DefaultExt = DefaultExt,
                Filter = Filter
            };

            var result = dialog.ShowDialog();
            if (result == true)
            {
                if (FileSelected != null)
                {
                    FileSelected(this, dialog.FileName);
                }
                if (TextBox != null)
                {
                    TextBox.Text = dialog.FileName;
                }
            }
        }
    }
}

到現在為止還挺好。 我可以在XAML中快速創建一個“瀏覽...”按鈕。 但是 ,我無法以我希望它能正常工作的方式使TextBoxDependency工作。

我想要做的是這樣的(XAML):

<TextBox x:Name="MyTextBox" />
<extensions:BrowseButton TextBox="MyTextBox" />

但是,當我放下它時,它說:

“文本框”的TypeConverter不支持從字符串轉換。

有什么方法可以完成我想在這里做的事情嗎? 要有效地引用XAML元素內的另一個XAML元素,而不必離開XAML呢?

使用綁定:

<TextBox x:Name="MyTextBox" />
<extensions:BrowseButton TextBox="{Binding ElementName=MyTextBox}" />

暫無
暫無

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

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