开发者

Silverlight simulate mouse click by X, Y?

If there is a way to send mouse click event by location programatically it would be great, but if theres another approach that can solve following problem this it is fine too.

In my situation I got a canvas taking up whole application size (covering it completely) and when user clicks it with mouse I want to h开发者_StackOverflow社区ide it, and then pass through this mouse click (taking its location x & y from user) to anything that is under canvas (in my case canvas visibility goes to collapsed so controls under it can be seen now).

I am guessing it is impossible, cause certain features like run silverlight fullscreen can only be done in button click handler (correct me if im wrong here).

But is there a place where I can read about those security based limitations of silverlight UI ?


you have to add an click event handler to your canvas. In this handler you get the x and y positon of your click (via MouseButtonEventArgs) and then you can use the VisualTreeHelper to get your "hit elements".

Lets assume the following xaml:

 <Grid x:Name="LayoutRoot" Background="White">
        <Button  Width="50" Height="50" VerticalAlignment="Top" HorizontalAlignment="Left"/>
        <TextBox Text="MyText" Width="200" Height="100" VerticalAlignment="Top" HorizontalAlignment="Left"/>
        <Canvas Background="Red"  x:Name="MyCanvas"  />
    </Grid>

with the following code behind:

    public MainPage()
            {
                InitializeComponent();
                MyCanvas.AddHandler(MouseLeftButtonUpEvent, new MouseButtonEventHandler(handler), true);
            }

            void handler(object sender, MouseButtonEventArgs e)
            {
                var point = new Point(e.GetPosition(this).X, e.GetPosition(this).Y);
                var elements = VisualTreeHelper.FindElementsInHostCoordinates(point, this);
                foreach (var uiElement in elements)
                {
                    if (uiElement is TextBox){
                      ((TextBox) uiElement).Focus();
                      break;
                    }
                    if(uiElement is Button)
                    {
                      //do button stuff here
                      break;
                    }
                }
                MyCanvas.Visibility = Visibility.Collapsed;
                MyCanvas.RemoveHandler(MouseLeftButtonUpEvent, new MouseButtonEventHandler(handler));
            }

But: In this simple example, you get at about 20 hit elements. But they are sorted in the correct "z-Index". So you can iterate through it and the first interesting element for you is where you could break(Maybe you can do this with LINQ, too). So for me, I know that the first hit TextBox is what I want to focus.

Is this what you need?

BR,

TJ

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜