The following code is used to directly set value of property that is bound to control's property. (In my case a property is bound to ValueProperty
of Slider
).
private Action<double> _valueSetter; // target property setter cache
private double _newValue;
protected override void OnThumbDragCompleted(DragCompletedEventArgs e)
{
if (_valueSetter == null)
{
var bindingExpr = BindingOperations.GetBindingExpression(this, ValueProperty);
var property = bindingExpr?.DataItem
.GetType()
.GetProperty(bindingExpr.ParentBinding.Path.Path);
_valueSetter = (Action<double>)property?
.GetSetMethod()
.CreateDelegate(typeof(Action<double>), bindingExpr.DataItem);
}
_valueSetter?.Invoke(_newValue); // as fast as possible!
base.OnThumbDragCompleted(e);
}
The problem is if I change the Binding of the ValueProperty
I don't know that happened and therefor my _valueSetter
is still referring to old property setter.
Obviously one way is to always use reflection without using _valueSetter
but is there better way? It would be really nice if I knew that ValueProperty
is just bound to new property and then I could update my _valueSetter
.
I tried to create another binding for dependency property but they are static and that confuses me. is there any way to fire an event when binding of control's property changes?
Aucun commentaire:
Enregistrer un commentaire