【发布时间】:2014-02-18 08:06:19
【问题描述】:
我有一个为TextBox 类定义一些自定义依赖属性的类:
public class DependencyProperties:FrameworkElement
{
public static readonly DependencyProperty SelectionBeginProperty = DependencyProperty.Register("SelectionBegin", typeof(int), typeof(TextBox),
new UIPropertyMetadata(0, SelectionStartDependencyPropertyChanged));
public static readonly DependencyProperty SelectionLengthProperty = DependencyProperty.Register("SelectionLength", typeof(int), typeof(TextBox),
new UIPropertyMetadata(0, SelectionLengthDependencyPropertyChanged));
public static readonly DependencyProperty ChildrenProperty = DependencyProperty.Register("Children", typeof (string), typeof (TreeView));
static DependencyProperties()
{
}
...
}
当我尝试在 Xaml 中使用这些属性时:
<TextBox Name="TextBox_1735"
SelectionBegin="{Binding TextBox_1735SelectionBegin, UpdateSourceTrigger=PropertyChanged}"
SelectionLength="{Binding TextBox_1735SelectionLength, UpdateSourceTrigger=PropertyChanged}" />
它会引发属性SelectionBegin 无法解析的异常。
【问题讨论】:
-
在您的情况下,您需要实现 attached 依赖属性 -
MSDN link。并像这样使用:<TextBox local:DependencyProperties.SelectionBegin={...} />. -
我试过了。但现在我得到另一个错误:“不能在'TextBox'类型的'SetSelectionBegin'属性上设置'Binding'。只能在DependencyObject的DependencyProperty上设置'Binding'。”
-
@Sergiu 你在实现custom attached property..时在某个地方犯了一个错误。
标签: c# wpf textbox dependency-properties