我正在努力克服不允许我绑定到常规clr属性的限制。
我使用的解决方案使用自定义依赖项属性,后者又会更改clr属性。
这是代码
class BindableTextBox : TextBox
{
public static readonly DependencyProperty BoundSelectionStartProperty = DependencyProperty.Register("BoundSelctionStart", typeof(int), typeof(BindableTextBox),
new PropertyMetadata(new PropertyChangedCallback(BindableTextBox.onBoundSelectionStartChanged)));
private static void onBoundSelectionStartChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
((TextBox)d).SelectionStart = (int)e.NewValue;
}
private static readonly DependencyProperty BoundSelectionLenghtProperty = DependencyProperty.Register("BoundSelectionLenght", typeof(int), typeof(BindableTextBox),
new PropertyMetadata(new PropertyChangedCallback(BindableTextBox.onBoundSelectionLenghtChanged)));
private static void onBoundSelectionLenghtChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
((TextBox)d).SelectionLength = (int)e.NewValue;
}
public int BoundSelectionStart
{
get { return (int)GetValue(BoundSelectionStartProperty); }
set { SetValue(BoundSelectionStartProperty, value); }
}
public int BoundSelectionLenght
{
get { return (int)GetValue(BoundSelectionLenghtProperty); }
set { SetValue(BoundSelectionLenghtProperty, value); }
}
}
但是当我尝试将某些东西绑定到BoundSelectionStart时,它说它只能绑定到DP。
<bindable:BindableTextBox Text="{Binding Name}" BoundSelectionStart="{Binding ElementName=slider1, Path=Value}" />
有什么问题?
答案 0 :(得分:2)
你的行中有一个拼写错误:
public static readonly DependencyProperty BoundSelectionStartProperty = DependencyProperty.Register(...)
第一个参数应为“BoundSelectionStart”(选择中为2x e),而不是“BoundSelctionStart”。