Proprietà di dipendenza WPF non riconosciuta
-
06-07-2019 - |
Domanda
Sto cercando di superare una limitazione che non mi consente di legare alle normali proprietà clr.
La soluzione che utilizzo utilizza proprietà di dipendenza personalizzate che a loro volta cambiano le proprietà clr.
Ecco il codice
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); }
}
}
Ma quando provo a legare qualcosa a BoundSelectionStart, dice che posso solo legare a DP.
<bindable:BindableTextBox Text="{Binding Name}" BoundSelectionStart="{Binding ElementName=slider1, Path=Value}" />
Qual è il problema?
Soluzione
Hai un refuso nella riga:
public static readonly DependencyProperty BoundSelectionStartProperty = DependencyProperty.Register(...)
Il primo parametro dovrebbe essere " BoundSelectionStart " (2x e in Selezione), non " BoundSelctionStart " ;.
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow