简体   繁体   中英

Can I create a DependencyProperty that accepts a XAML element?

I created a custom class called BrowseButton which extends Button . This button is fairly simple; when clicked it pops up a file chooser dialog. I created it as its own special class because I wanted to be able to re-use it quickly and easily in my applications. After the user successfully selects a file, I also want it to populate a TextBox control on the same page with the full file path.

Here's what my (C#) code looks like for the button:

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;
                }
            }
        }
    }
}

So far, so good. I can quickly create a "Browse..." button in XAML. However , I can't get the TextBoxDependency working in the way that I was hoping it would work.

What I want to be able to do is something like this (XAML):

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

However, when I drop that in it says this:

The TypeConverter for "TextBox" does not support converting from a string.

Is there some way to accomplish what I want to do here? To effectively reference another XAML element inside of a XAML element, without having to leave XAML to do it?

Use a binding:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM