【发布时间】:2011-11-03 14:58:35
【问题描述】:
出于测试目的,我在 DLL 中编写了 Silverlight 自定义控件,该控件仅包含在包含 ListBox 的 Grid 中,预填充了 2 个项目。
(仅供参考:在这个项目中,我只有 2 个文件:BaseLayout.cs 和 Generic.xaml。)
然后,在另一个项目中,我将这个自定义控件放在我的 MainPage.xaml 中:
<UserControl x:Class="Test.MainPage"
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:base="clr-namespace:Base;assembly=Base"
mc:Ignorable="d"
d:DesignHeight="427"
d:DesignWidth="508">
<base:BaseLayout x:Name="MyLayout"
ListBoxBg="BlanchedAlmond" />
</UserControl>
我知道如何使用 TemplateBinding 更改 ListBox 的背景颜色。
我也知道如何在运行时向这个 ListBox 添加项目。
但是是否可以在设计时在 XAML 中添加项目? 有没有办法允许这样的事情:
<base:BaseLayout x:Name="MyLayout"
ListBoxBg="BlanchedAlmond">
<ListBoxItem Content="Item 4" />
<ListBoxItem Content="Item 5" />
</base:BaseLayout>
欢迎提出任何建议。
编辑:更准确地说,我希望能够从两个地方在我的 ListBox 中添加一些项目:
- 来自 Base.DLL,在 Generic.xaml 中,
- 来自另一个项目,在 MainPage.xaml 中。
我希望所有这些项目都保留在运行时。
【问题讨论】:
标签: silverlight xaml custom-controls design-time