I create a DependencyProperty as follow in a UserControl, but when i try to bind a value to in it is not working. I would be grateful if you could explain to me why it is not working and how to fix that. Regards!
private bool _userChangeBaseValue;
public static readonly DependencyProperty BaseValueProperty = DependencyProperty.Register("BaseValue", typeof(double?), typeof(WPFTextBoxUnitConverter),
new PropertyMetadata(null, new PropertyChangedCallback(OnSetBaseValueChanged)));
[Category("TextBoxUnitConverter")]
[Description("BaseValue")]
public double? BaseValue
{
get
{
return (double?)GetValue(BaseValueProperty);
}
set
{
SetValue(BaseValueProperty, value);
}
}
private static void OnSetBaseValueChanged(DependencyObject dependencyObject, DependencyPropertyChangedEventArgs e)
{
WPFTextBoxUnitConverter wpfTextBoxUnitConverter = dependencyObject as WPFTextBoxUnitConverter;
wpfTextBoxUnitConverter.OnSetBaseValueChanged(e);
}
private void OnSetBaseValueChanged(DependencyPropertyChangedEventArgs e)
{
if (_userChangeBaseValue)
{
txtUnitCoverter.TextChanged -= txtUnitCoverter_TextChanged;
double? newValue = e.NewValue as double?;
ValueManager.DoubleValue = newValue;
if (newValue.HasValue)
{
ValueManager.DoubleValue = ConverterToolBox.Convert(newValue.Value, BaseUnit, CurrentUnit);
}
double testIsConverteAble;
bool isCanConvert = double.TryParse(txtUnitCoverter.Text, out testIsConverteAble);
bool isEmpty = string.IsNullOrEmpty(txtUnitCoverter.Text) || string.IsNullOrWhiteSpace(txtUnitCoverter.Text);
// Validation...
ValidationResult validationResult = new ValidationResult();
UnitConverterTextBoxValidation?.Invoke(this, isEmpty, isCanConvert, txtUnitCoverter.Text, BaseNumberUnit, CurrentNumberUnit, validationResult);
ValueManager.IsValueValid(validationResult);
txtUnitCoverter.TextChanged += txtUnitCoverter_TextChanged;
}
}