用椭圆突出显示WPF中的鼠标位置 [英] Highlighting mouse position in wpf with an ellipse
问题描述
你好,
我在实现wpf窗口中的一项功能时遇到了一些问题.我在填充整个窗口的网格内有一个图表控件(让它成为任何控件...).我想使用椭圆突出显示图表中的鼠标位置.
我尝试了以下-
1.我保留了一个画布而不是网格-现在我的图表控件不再可以拉伸以填充窗口
2.我保留了网格并在其中保留了Ellipse和Chart控件.尝试将Ellipse的ZIndex更改为0,将Chart控件更改为1.但是尽管可见性为Visible并且ZIndex为0,但是在运行时看不到Ellipse.
到目前为止,我什至找不到一个好的解决方案.我知道在Windows窗体中它非常容易,但是在WPf中却发现非常困难.请提供带有代码段的解决方案.
我当前的代码看起来像这样....
xaml:
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....
推荐答案
阿西什!
Z索引由可视树中元素的顺序确定,因此椭圆位于图表的后面.此外,较高的z索引显示在较低的z索引上方,因此您将它们以错误的方式绕过.
我发现此XAML可以正常工作:
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:
<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);
}
这篇关于用椭圆突出显示WPF中的鼠标位置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!