My Silverlight series (8) -- about ComboBox in Silverlight 2 RC0

Source: Internet
Author: User

on the 25 th of last month, Microsoft released Silverlight 2 RC0, before the official RTW release of silverlight2, A transitional version provided by Program . In this version, Microsoft's Silverlight team made three general controls for us: progressbar, ComboBox, and passwordbox. Relatively speaking, the use of these three controls is relatively simple, and it is not even more difficult than the button. Today, I do not need to explain how to use these controls, I just want to talk about a small technique that Microsoft uses to implement ComboBox.

note a feature of ComboBox, that is, in the dropdown state, if a user clicks a location other than the dropdown of comboxbox, the dropdown should be automatically collected. Since the container implementing dropdown is a popup, you only need to change the isopen attribute of this popup. But the key to the problem is when to change it and when it should be triggered. As mentioned above, the user clicks a place other than dropdown, which is very tricky, because we are not necessarily clicking somewhere in dropdown, you cannot subscribe to an event within dropdown. I used to think about how to do this through the lostfocus event. However, after trying, I found it was not feasible. The event was not triggered when I clicked somewhere else. On the Silverlight official website, I have discussed this issue with a few people who have proposed to use mouseleave to add a timer, just like some JavaScript dropdown, as long as the mouse moves out of the dropdown area, it will be closed in a short period of time. However, this is not a good solution, and in winform, no one will handle it like this. Two days ago after I upgraded RC0, I was pleasantly surprised to find that Microsoft's ComboBox has this function, and I went to study it. In the end, he found that when he was doing this dropdown, he made a transparent canvas for the dropdown to occupy the entire application area, and added the mouseleftbuttondown event to it. Because the mouseleftbuttondown event of Silverlight cannot be clicked-through, other controls do not respond, but are intercepted and processed by the transparent canvas. In fact, this workaround also has its drawbacks. if you carefully study winform dropdown, you can find that if you click a button elsewhere, in addition to the dropdown hidden, the button event is also triggered, but the ComboBox that Microsoft does so will only respond when it is clicked again after it is collected. However, compared with Microsoft, this is already a good solution, at least it will not be a bug that dropdown does not follow when we move ComboBox.

In addition, this idea should not be original to Microsoft. when discussing the implementation of modal-dialog, someone proposed a transparent canvas to avoid the showdialog issue, the control below will still respond to the mouse click event. However, this method is not perfect. If you don't work with the mouse, it doesn't mean that the keyboard won't work. In short, there is a way to make you uncomfortable.

Therefore, what I suggest MSFT is to try to find a better solution, mainly in terms of message mechanism, so that Silverlight is more like WPF/E than just an imitation. Despite the difficulties, I think it is just around the corner.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.