【问题标题】:Can I create a DependencyProperty that accepts a XAML element?我可以创建一个接受 XAML 元素的 DependencyProperty 吗?
【发布时间】:2015-08-09 20:33:48
【问题描述】:

我创建了一个名为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" />

但是,当我把它放进去时,它会说:

“TextBox”的 TypeConverter 不支持从字符串转换。

有什么方法可以完成我想在这里做的事情吗?要有效地引用 XAML 元素内的另一个 XAML 元素,而不必离开 XAML 来执行它?

【问题讨论】:

    标签: c# wpf xaml dependency-properties


    【解决方案1】:

    使用绑定:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-07-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多