[英]Can I create a DependencyProperty that accepts a XAML element?
I created a custom class called BrowseButton
which extends Button
. 我创建了一个名为
BrowseButton
的自定义类,该类扩展了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. 用户成功选择文件后,我还希望它使用完整的文件路径在同一页面上填充
TextBox
控件。
Here's what my (C#) code looks like for the button: 这是该按钮的(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;
}
}
}
}
}
So far, so good. 到现在为止还挺好。 I can quickly create a "Browse..." button in XAML.
我可以在XAML中快速创建一个“浏览...”按钮。 However , I can't get the
TextBoxDependency
working in the way that I was hoping it would work. 但是 ,我无法以我希望它能正常工作的方式使
TextBoxDependency
工作。
What I want to be able to do is something like this (XAML): 我想要做的是这样的(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.
“文本框”的TypeConverter不支持从字符串转换。
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?
要有效地引用XAML元素内的另一个XAML元素,而不必离开XAML呢?
Use a binding: 使用绑定:
<TextBox x:Name="MyTextBox" />
<extensions:BrowseButton TextBox="{Binding ElementName=MyTextBox}" />
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.