我的目标是扩展已设置的对象样式.假设我有以下两种风格:
<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!
我试图动态构造一个新的Style并添加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." }
解决方法
“BasedOn”是否适用于Silverlight? // wpf开发者,永远不会确定
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。