开发者

Finding the bound control from a Binding

I've made a custom binding class which inherits from Binding:

public class MyBinding : Binding
{
  public JsBinding(): base()
  {
    InitiateBinding();
  }

  public JsBinding(string path) : base(path)
  {
    InitiateBinding();
  }

  private void InitiateBinding()
  {
    this.ValidatesOnDataErrors = true;
    this.ValidatesOnExceptions = true;
    this.UpdateSourceTrigger = UpdateSourceTrigger.PropertyChanged;
    UpdateSourceExceptionFilter = UpdateSourceExceptionFilterCallback;
  }

  private object UpdateSourceExceptionFilterCallback(object bindExpression, Exception exception)
  {
    // Set focus back to bound framework element
    // Setup PreviewKeyDown and PreviewLostKeyboardFocus on bound framework element to prevent further u开发者_运维百科ser input until data has become valid
  }

}

The bound control is supposed to be a Framework element.

I need to access it directly to setup these events: PreviewKeyDown and PreviewLostKeyboardFocus.

But how do I access the framework element through the binding? Can this be done?

I've tried Keyboard.FocusedElement but using telerik's radnumericupdown have shown me that this may not be the same element as the binding binds to.


I'm guessing you are setting up your bindings in Xaml otherwise you would just be able to construct your custom Binding class with the control instance it is being applied to.

We had a similar problem to this and what we ended up doing was creating a custom BindingExtension which inherits from MarkupExtension. We then use that in place of a regular Binding in Xaml - e.g. {helpers:Binding} instead of {Binding}. What this gives you is that the MarkupExtension gets access to the dependency object to which it is bound when it initializes (in ProvideValue) - you can then set control properties, hook events as required and / or create your custom Binding class instead of the regular one and give it a handle to the control to which it is bound.

I believe we got the initial sample code for a custom Binding MarkupExtension and how you can override ProvideValue to get access to the target control here: http://www.hardcodet.net/2008/04/wpf-custom-binding-class

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜