Workaround for some WPF features not found in Silverlight

I am porting a WPF application to silverlight 2 and come across several WPF features that are not currently available in SL. Can someone help me with equivalents or suggest workarounds.

  • I want to handle clicks and double clicks on a text box embedded in a list. The WPF implementation uses PreviewMouseLeftButtonDown / Up for the list control. As it can be done in silverlight, it seems that in Silverlight there is no PreviewMouseLeftButtonDown / Up.

  • I want to handle button presses (F2 / Delete) in a text box embedded in a list. The WPF implementation uses PreviewKeyDown for a text box control that is embedded as an item in a list. PreviewKeyDown seems to be missing in silverlight. The KeyDown event handler does not seem to be called.

  • I want to change some appearance properties of a text field depending on the value of some custom properties. To implement WPF, DataTrigger is used. How to do it in Silverlight. It seems DataTriggers are missing in silverlight.

  • I want to change the width of the text field depending on the actual width of the list that contains the text field. The WPF implementation uses the RelativeSource binding. What is the Silverlight equivalent, or a workaround for this.

+7
wpf silverlight
source share
3 answers

For items 1 and 2, the best way to access these input events is to create a custom TextBox obtained from the embedded TextBox. Then you can override OnKeyDown and OnMouseLeftButton. From there, you can either call the desired code, or start a new event. eg:.

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.

0
source share

I am more familiar with Silverlight than with full WPF. Please agree with my answers accordingly.

For number 2. For many keys, I check KeyUp and KeyDown. I use KeyDown, trying to watch all the time when the key is held, and KeyUp when it was used only once. You should know that this was for a game without a separate text field.

0
source share

For element 4, you can bind the width of the list and the width of the text field to the property of the static resource so that it acts as a router for the binding. You can also use the value converter that you initialize with the list link, then use the converter for your text box width.

For item 3, you can use a similar approach.

0
source share

All Articles