将usercontrol添加到另一个用户控件

时间:2010-10-20 17:41:28

标签: silverlight-4.0

这是我的用户控件: -

<UserControl
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    xmlns:ed="http://schemas.microsoft.com/expression/2010/drawing"
    mc:Ignorable="d"
    x:Class="NiceAnimation.ArcUserControl"
    d:DesignWidth="72" d:DesignHeight="72">

    <Grid x:Name="LayoutRoot">
        <ed:Arc x:Name="arcControl" ArcThickness="20" ArcThicknessUnit="Pixel" EndAngle="360" Fill="#FFFF8700" Stretch="None" Stroke="Black" StartAngle="0" UseLayoutRounding="False" StrokeThickness="0"/>
    </Grid>
</UserControl>

在我的MainPage.xaml.cs中,我正在动态创建用户控件,但它没有被创建。甚至也没有例外。

public partial class MainPage : UserControl
    {
        public MainPage()
        {
            // Required to initialize variables
            InitializeComponent();
            this.Loaded += new RoutedEventHandler(MainPage_Loaded);
        }

        void MainPage_Loaded(object sender, RoutedEventArgs e)
        {
            Random objRandom = new Random();
            for (int intIndex = 0; intIndex < 30; intIndex++)
            {
                ArcUserControl objArcUserControl = new ArcUserControl();
                objArcUserControl.Opacity = 0.7;
                //objArcUserControl.SetValue(Canvas.LeftProperty, (double) objRandom.Next(10, 640));
                //objArcUserControl.SetValue(Canvas.TopProperty, (double)objRandom.Next(10, 480));
                objArcUserControl.MouseEnter += new MouseEventHandler(objArcUserControl_MouseEnter);  
                //MainCanvas.Children.Add(objArcUserControl);
                canLayer1.Children.Add(objArcUserControl);

               // canLayer2.Children.Add(objArcUserControl);
               // canLayer3.Children.Add(objArcUserControl);
            }

        }

        void objArcUserControl_MouseEnter(object sender, MouseEventArgs e)
        {
            MessageBox.Show(e.GetPosition(this).ToString());
        }
    }

我缺少什么?此外,如果我添加textbox而不是usercontrol它可以工作。 在此先感谢:)

1 个答案:

答案 0 :(得分:-1)

尝试在xaml中添加ArcUserControl并查看它是否呈现。什么是canLayer1?