我需要在 Silverlight 中将 UserControl 创建为 ContainerControl(或模板),以便我可以在其他 Silverlight 页面中使用以在其中添加控件....
任何答案都会很明显......谢谢GK Prajapati
我需要在 Silverlight 中将 UserControl 创建为 ContainerControl(或模板),以便我可以在其他 Silverlight 页面中使用以在其中添加控件....
任何答案都会很明显......谢谢GK Prajapati
您要创建的是Silverlight Templated Control
(扩展ContentControl
)。
快速入门:
C# MyControl.cs
public class MyControl: ContentControl
{
public MyControl()
: base()
{
this.DefaultStyleKey = typeof(MyControl);
}
public override void OnApplyTemplate()
{
base.OnApplyTemplate();
}
}
XAML MyControl.xaml
<ResourceDictionary
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:MyControlNsp="clr-namespace:My.Control.NameSpace">
<Style TargetType="UserToolkit:WarningBar">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="MyControlNsp:MyControl">
<Border>
<ContentControl
x:Name="content"
Content="{TemplateBinding Content}"
ContentTemplate="{TemplateBinding ContentTemplate}"
HorizontalContentAlignment="{TemplateBinding HorizontalContentAlignment}"
VerticalContentAlignment="{TemplateBinding VerticalContentAlignment}"/>
</Border>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</ResourceDictionary>
编辑:
如果不同的控件,
您可以将ItemsControl与集合一起使用:
var myControl = new ItemsControl();
myControl.Items.Add(new TextBlock { Text = "My text block" });
myControl.Items.Add(new CheckBox { Content = "My check box" });
// Or with a collection of items
ObservableCollection<object> controls = new ObservableCollection<object>();
controls.Add(new TextBlock { Text = "My text block" });
controls.Add(new CheckBox { Content = "My check box" });
var myControl2 = new ItemsControl();
myControl2.ItemsSource = controls;