troubleshooting Question

How to find focused control in Silverlight

Avatar of Joni Kettunen
Joni KettunenFlag for Finland asked on
C#Microsoft Development
2 Comments1 Solution1041 ViewsLast Modified:
I'm writing copy / paste functionality to a silverlight project. My project contains multiple pages, TextBox objects etc.

Now user wants to copy and paste selected text.

My problem is that I can't find which object has focus and without that information it is hard to copy text.

My xaml page has a grid which contains some user controls in different cells. The main grid handles right mouse menu with copy/paste functionality.

Since I am not working with WinForms, but in Silverlight. How can I find focused control?

Here are some code snippets:
<!-- Right mouse click opens copy/paste menu -->
<!-- Left mouse click just closes it -->
<Grid x:Name="LayoutRoot" Background="White" Loaded="LayoutRoot_Loaded" 

<!-- Here are lots of elements created -->  

        <Popup x:Name="ContextMenu" Visibility="Collapsed" MouseRightButtonDown="LayoutRoot_MouseLeftButtonDown">
            <Border CornerRadius="5" BorderBrush="White" BorderThickness="1" Background="LightGray">
                <StackPanel >
                    <Button Content="Copy" Foreground="Black" Click="CopySelectedContent" />
                    <Button Content="Paste" Foreground="Black" Click="PasteFromClipboard" />

<!-- In code beside file there is a event handler -->
private void LayoutRoot_MouseRightButtonDown(object sender, MouseButtonEventArgs e)
    // I'd perefer capturing focused control here - before right mouse menu (canvas) is opened.
    // But sender is Grid hosting this canvas.
    Point position = e.GetPosition(null);
    e.Handled = true;
    ContextMenu.Visibility = Visibility.Visible;
    ContextMenu.IsOpen = true;
    ContextMenu.SetValue(Canvas.LeftProperty, (double)position.X);
    ContextMenu.SetValue(Canvas.TopProperty, (double)position.Y);  
Join the community to see this answer!
Join our exclusive community to see this answer & millions of others.
Unlock 1 Answer and 2 Comments.
Join the Community
Learn from the best

Network and collaborate with thousands of CTOs, CISOs, and IT Pros rooting for you and your success.

Andrew Hancock - VMware vExpert
See if this solution works for you by signing up for a 7 day free trial.
Unlock 1 Answer and 2 Comments.
Try for 7 days

”The time we save is the biggest benefit of E-E to our team. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange.

-Mike Kapnisakis, Warner Bros