jueves, 15 de octubre de 2009

Implementing CustomVisualStateManagers to see in which visual state is your object

Technology: Silverlight 3.0

Problem: I need to know which method from Silverlight framework changes the VisualState of a control and when.

Solution: Develop a Custom VisualStateManager and override the GoToStateCore method (which is the only method that can be overriden), doing a Debug.WriteLine with Debug information. I decided to write the information on debug instead of setting a breakpoint, because the breakpoint might alter the behavior of the VisualStateManager. Another thing to remember is to call the base method when overriding GoToStateCore.

In order to set the CustomVisualStateManager as the default VisualStateManager to handle the changes, you can set the XAML in this way:

You could also set it through the static method: VisualStateManager.SetCustomVisualStateManager(FrameworkElement,VisualStateManager);

martes, 13 de octubre de 2009

Silverlight 3.0 - LostFocus in Popup Controls

Technology: Silverlight 3.0

Problem: I want to close a Popup when a Custom Control that contains the popup losses focus.

Considerations:

LostFocus is a Routed Event

This means that if a child triggers a lostFocus event, it will bubble up to the parent. I don’t think this makes too much sense because if a focus changed from a child to another child of the same parent, then the parent should not have a LostFocus event triggered (because the focus is still in one of its children)…but that is how it works.

LostFocus and the Popup control

When one of the Children is a popup, the LostFocus event won’t bubble up to Popup the container (This happens for the LostFocus and for other routed events as well).

Example: If I click on tst2.1, and then on tst2.2, the event LostFocus will be triggered for the stack panel SP2, but it won’t trigger for SP1.


Solution:

There are two things to address, 1) The fact that the LostFocus event will not get to the parent control, and 2) The fact that lostFocus being triggered does not mean that the control lost focus (but one of its children did).

To solve 1), have a panel container inside the Popup, and register to the lostFocus event in that control. You know that all the lostFocus for the popup children will trigger the container LostFocus.

To solve 2), in the LostFocus event for your control, check if the current focused element (FocusMager.GetFocusedElement) is inside your control. You can do this using VisualTreeHelper class.

martes, 6 de octubre de 2009

Binding validation failed because the binding's MsmqAuthenticationMode property is set to WindowsDomain but MSMQ is installed with Active Directory in


Technology: Message Queues with WCF

Problem: After implementing a MQ service with WCF I got the error "Binding validation failed because the binding's MsmqAuthenticationMode property is set to WindowsDomain but MSMQ is installed with Active Directory integration disabled."

Solution: In this case I did not need security, so I just configured the "Security" TAB in a custom binding configuration, setting MSMQProtectionLevel to None and MSMQAuthenticationMode to None.