Click here to Skip to main content
15,891,657 members
Please Sign up or sign in to vote.
4.50/5 (2 votes)
See more:
Hello there,

i am facing some issue to implement one feature in my wpf window. I have a chart control (let it be any control...) inside the Grid which fills the entire window. I want to highlight the mouse position in the chart using an ellipse.

I tried the following -

1. I kept a canvas instead of grid - Now my chart control is no longer stretchable to fill the window

2. I kept the Grid and kept both Ellipse and Chart control inside it. Try changing the ZIndex of Ellipse to 0 and Chart control to 1. But could not see the Ellipse at run time despite visibility is Visible and ZIndex is 0.

I could not find even a single good solution so far. I know its very easy in Windows forms, but found very difficult in WPf. Kindly provide a solution with code snippet.

My Current code look like this....

xaml:

<Grid  Background="White"  AllowDrop="True" Name="myGrid" >
     <Ellipse Visibility="Hidden" Name="ellipse1" Stroke="Black" Height="10" HorizontalAlignment="Left" VerticalAlignment="Top" Width="10" Fill="Red"/>
     <d3:ChartPlotter Name="plotter" Margin="0,43,0,0" Panel.ZIndex="1">
     </d3:ChartPlotter>
 </Grid>


code:

void plotter_MouseMove(object sender, MouseEventArgs e)
  {
      Point pt = new Point(e.GetPosition(this).X, e.GetPosition(this).Y);
      ellipse1.Visibility = Visibility.Visible;
      ellipse1.Margin = new Thickness(pt.X, pt.Y, this.ActualWidth - pt.X, this.ActualHeight - pt.X);
  }



Many thanks....
Posted

1 solution

Hi Asish!

The z-index is determined by the order of elements in the visual tree, so your ellipse is behind your chart. Additionally, higher z-indexes are displayed above lower z-indexes so you have them the wrong way round.

I found this XAML to work:

XML
<Grid MouseMove="Grid_MouseMove" Name="grid">
        <TextBox Background="AliceBlue"/>
        <Ellipse Name="ellipse" HorizontalAlignment="Left" VerticalAlignment="Top" Width="10" Height="10" Fill="Aqua"/>
    </Grid>


C#:

C#
private void Grid_MouseMove(object sender, MouseEventArgs e)
        {
            var pos = e.GetPosition(grid);
            ellipse.Margin = new Thickness(pos.X, pos.Y, 0, 0);
        }
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 9-Apr-11 1:23am    
Looks very nice (did not check up, hope I can trust you)! My 5.
--SA

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900