最近,我把我的Silverlight应用程序从3升级到4。几个小时后,我把头撞在墙上,试图解决一个问题,我把问题缩小到了以下几个方面:
我有一个用户控件,其中包含一个ComboBox。ComboBox只有一个ComboBoxItem子级。用户控件公开一个get访问器,该访问器返回ComboBox的Items对象,允许我通过xaml添加额外的ComboBoxItems。
在Silverlight 3中,这一切都很好,但在Silverlight 4中却不起作用。
作为代码:
//XAML
<UserControl ... >
<ComboBox Name="myComboBox">
<ComboBoxItem Content="Select an Item" />
</ComboBox>
<!-- All my other stuff -->
</UserControl>
//Code behind
public ItemCollection ListItems
{
get
{
return myComboBox.Items;
}
}
//Implementation of User-Control
<CustomControl:UserControl ... >
<CustomControl:UserControl.ListItems>
<ComboBoxItem Content="Item 1" />
<ComboBoxItem Content="Item 2" />
<ComboBoxItem Content="Item 3" />
</CustomControl:UserControl.ListItems>
</CustomControl:UserControl>正如我提到的,在Silverlight 3中,这一切都很好,但在Silverlight 4中却不起作用。
的解决办法似乎是删除用户控件中的单个ComboBoxItem,但我希望避免,因为我希望将其作为默认项。
任何帮助都将不胜感激!
发布于 2010-04-19 22:30:05
XAML解析器是为Silverlight 4重新编写的,以使它与WPF更加一致。我非常肯定,您所期望的行为是SL3中的一个bug,我不认为它在WPF中会那样工作,尽管我从未真正尝试过。
您可能可以通过启用怪癖模式获得旧模式,但我不推荐它。相反,我要做的是为组合框创建一个ControlTemplate,以便在没有任何选择的情况下显示"select a item“文本。将其作为组合框中的一个实际项,实际上只是我们一直被迫使用Windows和HTML等技术的一次黑客攻击,但在Silverlight中,使用SelectedItem be null似乎更合适。
https://stackoverflow.com/questions/2671305
复制相似问题