R – Workaround for some WPF features that are missing in Silverlight

silverlightwpf

I’m porting a WPF app to silverlight 2, and have come across several WPF features which are presently missing from SL. Could anyone help me with equivalents or suggest workarounds.

  1. I want to handle clicks and double clicks on a textbox embedded in a list box. The WPF implementation uses PreviewMouseLeftButtonDown/Up on a listbox control. How can this be done in silverlight, it seems that PreviewMouseLeftButtonDown/Up are missing in silverlight.

  2. I want to handle button presses (F2/Delete) on a textbox embedded in a list box. The WPF implementation uses PreviewKeyDown on a textbox control which embedded as an item in a listbox. It seems that PreviewKeyDown is missing in silverlight. The KeyDown event handler does not seem to get invoked.

  3. I want to change some appearance properties of a textbox depending on the value of some custom attached properties. The WPF implementation uses a DataTrigger to do this. How can this be done in silverlight. It seems that DataTriggers are missing in silverlight.

  4. I want to change the width of a text box depending on the Actual Width of the listbox in which the text box is contained. The WPF implementation uses RelativeSource binding. What is the silverlight equivalent, or workaround for this.

Best Answer

For item 1 and 2, the best way to get access to these input events is to create a custom TextBox deriving from the built in TextBox. Then you can override the OnKeyDown and OnMouseLeftButton down. From there you can either call the necessary code, or fire a new event. e.g.:

public class MyTextBox : TextBox
{
    public event MouseButtonEventHandler MySpecialMouseLeftButtonDown;

    protected override void OnMouseLeftButtonDown(MouseButtonEventArgs e)
    {
        if (MySpecialMouseLeftButtonDown != null)
        {
            MySpecialMouseLeftButtonDown(this, e);
        }
        base.OnMouseLeftButtonDown(e);
    }
}

Similarly with OnKeyDown.

Related Topic