我得到“价值不在预期范围内”。当我尝试将自定义控件添加到堆栈面板时

时间:2011-03-30 02:08:10

标签: silverlight

我得到“价值不在预期范围内”。当我尝试将自定义控件添加到堆栈面板时。

我尝试为控件的名称指定一个唯一的名称,但无济于事。

以下是代码:

CurrentProjectControl currentProjectControl;

            foreach (var resource in ProjectResources)
            {
                //Random rand = new Random();
                currentProjectControl = new CurrentProjectControl(resource.ProjectCode, _projects, _timesheets, _projectResources);
                currentProjectControl.Name = (new Guid()).ToString();
                _scorecardView.CurrentProjectStackPanel.Children.Add(currentProjectControl);
            }

有没有人能对这个问题有所了解?

1 个答案:

答案 0 :(得分:0)

其实我发现了这个问题。它与Custom控件无关,声明如下:

currentProjectControl.Name = (new Guid()).ToString();

返回导致错误的相同结果。

所以正确的代码段是:

CurrentProjectControl currentProjectControl;

foreach (var resource in ProjectResources)
{
    currentProjectControl = new CurrentProjectControl(resource.ProjectCode, _projects, _timesheets, _projectResources);
    currentProjectControl.Name = Guid.NewGuid().ToString();
    _scorecardView.CurrentProjectStackPanel.Children.Add(currentProjectControl);
}