我可以创建一个接受 XAML 元素的 DependencyProperty 吗?
Can I create a DependencyProperty that accepts a XAML element?
我创建了一个名为 BrowseButton
的自定义 class,它扩展了 Button
。这个按钮相当简单;单击时会弹出一个文件选择器对话框。我将它创建为自己的特殊 class,因为我希望能够在我的应用程序中快速轻松地重新使用它。用户成功选择文件后,我还希望它用完整的文件路径在同一页面上填充一个 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 中快速创建一个 "Browse..." 按钮。 但是,我无法让 TextBoxDependency
以我希望的方式工作。
我想做的事情是这样的(XAML):
<TextBox x:Name="MyTextBox" />
<extensions:BrowseButton TextBox="MyTextBox" />
然而,当我把它放进去时,它是这样说的:
The TypeConverter for "TextBox" does not support converting from a string.
有什么方法可以完成我想在这里做的事情吗?要在 XAML 元素内有效地引用另一个 XAML 元素,而不必离开 XAML 来完成它?
使用绑定:
<TextBox x:Name="MyTextBox" />
<extensions:BrowseButton TextBox="{Binding ElementName=MyTextBox}" />
我创建了一个名为 BrowseButton
的自定义 class,它扩展了 Button
。这个按钮相当简单;单击时会弹出一个文件选择器对话框。我将它创建为自己的特殊 class,因为我希望能够在我的应用程序中快速轻松地重新使用它。用户成功选择文件后,我还希望它用完整的文件路径在同一页面上填充一个 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 中快速创建一个 "Browse..." 按钮。 但是,我无法让 TextBoxDependency
以我希望的方式工作。
我想做的事情是这样的(XAML):
<TextBox x:Name="MyTextBox" />
<extensions:BrowseButton TextBox="MyTextBox" />
然而,当我把它放进去时,它是这样说的:
The TypeConverter for "TextBox" does not support converting from a string.
有什么方法可以完成我想在这里做的事情吗?要在 XAML 元素内有效地引用另一个 XAML 元素,而不必离开 XAML 来完成它?
使用绑定:
<TextBox x:Name="MyTextBox" />
<extensions:BrowseButton TextBox="{Binding ElementName=MyTextBox}" />