如何在Silverlight中合并样式?
我的目标是扩展对象的已设置样式。假设我有以下两种风格:如何在Silverlight中合并样式?,silverlight,silverlight-3.0,styles,Silverlight,Silverlight 3.0,Styles,我的目标是扩展对象的已设置样式。假设我有以下两种风格: <Style TargetType="Ellipse" x:Key="OriginalStyle"> <Setter Property="Fill" Value="Blue"/> <Setter Property="Width" Value="100"/> <Setter Property="Height" Value="200"/> </Style> &l
<Style TargetType="Ellipse" x:Key="OriginalStyle">
<Setter Property="Fill" Value="Blue"/>
<Setter Property="Width" Value="100"/>
<Setter Property="Height" Value="200"/>
</Style>
<Style TargetType="Ellipse" x:Key="NewStyle">
<Setter Property="Fill" Value="Red"/>
</Style>
我尝试动态构建一个新样式,并添加NewStyle和OldStyle的属性-但是样式的属性始终为空,因此这会导致一个死胡同:
Style combinedStyle = new Style();
foreach (Setter setter in Old.Setters)
{
combinedStyle.Setters.Add(setter); // Get exception "Element is already the child of another element."
}
foreach (Setter setter in NewStyle.Setters)
{
combinedStyle.Setters.Add(setter); // Get exception "Element is already the child of another element."
}
似乎无法在Silverlight中动态合并样式。有人能证实这一点或向我展示一种更好的实现合并的方法吗?在Silverlight中“BasedOn”是否起作用wpf开发者,永远不要确定你可以这样做:-
<Style TargetType="Ellipse" x:Key="OriginalStyle">
<Setter Property="Fill" Value="Blue"/>
<Setter Property="Width" Value="100"/>
<Setter Property="Height" Value="200"/>
</Style>
<Style TargetType="Ellipse" x:Key="NewStyle" BasedOn="{StaticResource OriginalStyle}">
<Setter Property="Fill" Value="Red"/>
</Style>
请注意,这类Style
元素的出现顺序很重要,不能基于XAML解析器尚未处理的内容创建样式
<Style TargetType="Ellipse" x:Key="OriginalStyle">
<Setter Property="Fill" Value="Blue"/>
<Setter Property="Width" Value="100"/>
<Setter Property="Height" Value="200"/>
</Style>
<Style TargetType="Ellipse" x:Key="NewStyle" BasedOn="{StaticResource OriginalStyle}">
<Setter Property="Fill" Value="Red"/>
</Style>