如何在XAML中找到元素(形状)?

时间:2015-03-15 02:07:47

标签: c# xaml windows-phone-8.1

   private void DrawView_Tapped(object sender, TappedRoutedEventArgs e)
    {
        DrawView.ReorderMode = ListViewReorderMode.Enabled;
     var Location=   e.GetPosition(null);
     Node MyNode = new Node ();
     MyNode.Position.TranslateX = Location.X;
     MyNode.Position.TranslateY = Location.Y;

     GridPainter.Children.Add(MyNode.Circle);

} //Event that i am trying to use ,GridPainter is jut a Grid

public class Node
{
   public SolidColorBrush SelectedColor { get; set; }
   public Ellipse Circle { get; set; }
    public float XPosition { get; set; }
    public float YPosition { get; set; }
    public CompositeTransform Position { get; set; }
    public Node()
    {
        SelectedColor = new SolidColorBrush(Colors.Red);
        Circle = new Ellipse {Fill=SelectedColor,Width=30,Height=30 };
        Position = new CompositeTransform();
        Circle.RenderTransform = Position;
    }

}//The class that i am using

我想做的是在用户点击屏幕的位置画一个椭圆,但由于某种原因我的方法无效。我错过了什么?这在C#代码中是否可行?这是我的XAML代码

<Page
x:Class="Graph_Painter.MainPage"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:Graph_Painter"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d"
Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">

<Grid x:Name="GridPainter" 
      Tapped="DrawVi`enter code here`ew_Tapped"
       IsTapEnabled="True" >

</Grid>

1 个答案:

答案 0 :(得分:0)

CanvasShapes的更好容器。此外,如果只需要修改视觉形状的LeftTop,请考虑使用TranslateTransform作为视觉形状的RenderTransform属性。

您可以将上述代码改编为您的解决方案。

部分XAML(形状容器):

<Canvas x:Name="Canvas1"  Tapped="Canvas_Tapped" Background="White"/>

代码隐藏(Canvas tapped event handler):

private void Canvas_Tapped(object sender, TappedRoutedEventArgs e)
{
    var point = e.GetPosition(Canvas1);

    //example shape
    const double width = 30d;
    const double height = 30d;

    var ellipse = new Ellipse
    {
        Width = width,
        Height = height,
        Fill = new SolidColorBrush(Colors.Red),
        RenderTransform = new TranslateTransform
        {
            X = point.X - width/2,
            Y = point.Y - height/2
        }
    };

    Canvas1.Children.Add(ellipse);
}

我希望它有所帮助。