CoreCLR's Blog

Archive for December, 2009

Silverlight Feature Request: Allow Network Communication on Application Exit

Posted by coreclr on December 6, 2009

If you would like native .NET access to calling Web Services on Application Exit, then vote here:
http://silverlight.uservoice.com/pages/4325-runtime-feature-suggestions/suggestions/310806-allow-the-app-to-hit-a-web-service-on-exit?ref=title

Advertisements

Posted in .NET, Silverlight, Threading | Leave a Comment »

Silverlight Tip: Finding UI elements from mouse position

Posted by coreclr on December 6, 2009

Say you click on a UI element, and you want to know if you clicked a specific type.

Lets say you have this tree, and you want to know if you have clicked on a TabItem:

<Grid x:Name="LayoutRoot">
     <controls:TabControl Margin="100">
         <controls:TabItem Header="TabItem">
             <Grid/>
         </controls:TabItem>
         <controls:TabItem Header="TabItem">
             <Grid/>
         </controls:TabItem>
     </controls:TabControl>
 </Grid>

In this case we can use the FindElementsInHostCoordinates method. First we subscribe to the MouseLeftButtonUp event.

public MainPage()
{
   InitializeComponent();
   this.MouseLeftButtonUp +=  new MouseButtonEventHandler(MainPage_MouseLeftButtonUp);
}

We then get the  mouse position:

var position = e.GetPosition(null);


We can then use the FindElementsInHostCoordinates method and pass in the position. This will give us an IEnumerable of UIElement’s.

var elements = VisualTreeHelper.FindElementsInHostCoordinates(position, this);

We can now use a linq Query to select the TabItem elements:

var tabItems = from element in elements
               where element is TabItem
               select element;

And then we can test if there was a hit:

if (tabItems.Count() == 1)
{
     MessageBox.Show("TabItem was hit");
}

Enjoy.

Posted in Silverlight | Leave a Comment »