通常の 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}" />
何が問題ですか?