如何从代码后面访问动态对象到.xaml文件?

时间:2010-08-03 03:50:47

标签: c# .net wpf xaml

我是wpf的新手。我想为r1,r2,r3,r4,e1,e2应用动画。我必须在我的.xaml文件中访问这些对象名称以编写故事板,以便我可以应用动画。如何在.xaml文件中访问这些对象名称?您能否向我发送示例应用程序,以了解如何创建动态对象以及如何将其访问到.xaml中以应用动画

这是我的.cs文件的一部分

namespace WpfApplication1
{
    /// <summary>
    /// Interaction logic for Window1.xaml
    /// </summary>
    public partial class Window1 : Window
    {
        Rectangle r1, r2, r3, r4;
        Ellipse e1, e2;       
        Shape contact,lvi;
        int flag=0;
        Point startPoint,cp;
        private Point enterPoint,p;
        public Window1()
        {
            InitializeComponent();
        }

        .
        .
        .
        .
        .

     private void Canvas_Drop(object sender, DragEventArgs e)
    {
        if (e.Data.GetDataPresent("MyFormat"))
        {
            contact = e.Data.GetData("MyFormat") as Shape;
            Canvas CanvasView = sender as Canvas;
            //MessageBox.Show(contact.GetType().ToString());
            string savedObject = XamlWriter.Save(contact);
            StringReader stringReader = new StringReader(savedObject);
            XmlReader xmlReader = XmlReader.Create(stringReader);
            lvi = (Shape)XamlReader.Load(xmlReader);



            if (contact is Rectangle && contact.Name == "rect1" && r1 == null)
            {
                r1 = (Rectangle)lvi;
                r1.Margin = new Thickness(151, 129, 0, 0);
                CanvasView.Children.Add(r1);


            }
            else
            if (contact is Rectangle && contact.Name == "rect2" && r2 == null)
            {
                r2 = (Rectangle)lvi;
                r2.Margin = new Thickness(122, 142, 0, 0);
                canvas1.Children.Add(r2);
            }
            else
            if (contact is Rectangle && contact.Name == "rect3" && r3 == null)
            {
                r3 = (Rectangle)lvi;
                r3.Margin=new Thickness(132,225,0,0);
                canvas1.Children.Add(r3);
            }
            else 
            if (contact is Rectangle && contact.Name == "rect4" && r4==null )
            {
                r4 = (Rectangle)lvi;
                r4.Margin = new Thickness(177, 225,0,0);
                canvas1.Children.Add(r4);
            }
            else 
            if (contact is Ellipse && contact.Name == "ellipse1" && e1==null)
            {
                 e1 = (Ellipse)lvi;
                e1.Margin=new Thickness(123,74,0,0);
                canvas1.Children.Add(e1);
               // e1.MouseLeftButtonDown += new System.Windows.Input.MouseButtonEventHandler(ellipse_MouseLeftButtonDown);
            }
            else 
            if (contact is Ellipse && contact.Name == "ellipse2" && e2 == null)
            {
                e2 = (Ellipse)lvi;
                e2.Margin = new Thickness(324, 214, 0, 0);
                canvas1.Children.Add(e2);
            }
            else
            {
                MessageBox.Show("can not drop this item");
            }
        }
        }
}

}

1 个答案:

答案 0 :(得分:0)

在xaml中,您只能绑定到公共属性,或使用值转换器。