Ger*_*osz 4 silverlight styles silverlight-3.0
我的目标是扩展已设置的对象样式.假设我有以下两种风格:
<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>
Run Code Online (Sandbox Code Playgroud)
我想做的是将OriginalStyle分配给Ellipse,然后应用第二种样式只更改它影响的属性.理想情况下,我想做这样的事情:
Style OriginalStyle;
Style NewStyle;
Ellipse ellipse = new Ellipse();
ellipse .Style = OriginalStyle;
// Later in an event hanler
ellipse.Style = NewStyle; // I would want to keep the settings from the old style in here: in this example setting the style like this would make me lose the Width and Height properties!
Run Code Online (Sandbox Code Playgroud)
我试图动态地构造一个新的风格,并添加NewStyle和旧式的特性 - 不过的风格属性属性总是空,因此这导致了死胡同:
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."
}
Run Code Online (Sandbox Code Playgroud)
似乎无法在Silverlight中动态合并样式.有人可以证实这一点,或者告诉我一个更好的方法来实现合并吗?