Моя цель - расширить уже установленный стиль объекта. Предполагая, что у меня есть следующие два стиля:
<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>
То, что я хотел бы сделать, это назначить 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!
Я пытался динамически создать новый стиль и добавить свойства NewStyle и OldStyle - однако свойство Property стилей всегда имеет значение null, поэтому это приводит к тупику:
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. Может ли кто-нибудь подтвердить это или показать мне лучший подход к объединению?