无法在 Silverlight 样式中添加系统颜色?
我在 XAML 中为 SystemColors 定义了资源。如果我将 Foregroung 属性直接设置为 TextBlock,效果会很好。但是,如果我在样式中分配前景属性,则会收到如下所示的错误。我不确定问题是什么以及如何解决。任何想法都受到高度赞赏!
当我直接在 texblock 中设置前景时的代码。 它运行完美
<TextBlock Text="WindowTextColor" Foreground="{Binding WindowTextColor, Source={StaticResource SystemColors}, Converter={StaticResource colorConverter}}" />
当我通过样式设置前景属性时, 。我的应用程序崩溃:
<UserControl.Resources>
<local:ColorToBrushConverter x:Key="colorConverter" />
<local:SystemColorsWrapper x:Key="SystemColors" />
<Style x:Key="TextBlockStyle1" TargetType="TextBlock">
<Setter Property="Foreground" Value="{Binding WindowColor, Source={StaticResource SystemColors}, Converter={StaticResource colorConverter}}"/>
</Style>
</UserControl.Resources>
<Grid Background="#FFB8B8B8">
<TextBlock Text="WindowColor" Style="{StaticResource TextBlockStyle1}" />
</Grid>
我收到的错误:
发生 System.Windows.Markup.XamlParseException 消息 = 设置属性 '' 引发异常。 [线路:11 位置:41] 行号=11 线位置=41 堆栈跟踪: 在System.Windows.Application.LoadComponent(对象组件,Uri资源定位器) 在 SilverlightSysColors.MainPage.InitializeComponent() 在 SilverlightSysColors.MainPage..ctor() 内部异常:System.NotSupportedException 消息=无法设置只读属性“”。 堆栈跟踪: 在 MS.Internal.XamlMemberInfo.SetValue(对象目标,对象值) 在MS.Internal.XamlManagedRuntimeRPInvokes.SetValue(XamlTypeToken inType,XamlQualifiedObject&inObj,XamlPropertyToken inProperty,XamlQualifiedObject&inValue) 内部异常:
I defined resource in XAML for SystemColors. It is working great if I set Foregroung property directly to the TextBlock. However, I am getting an error shown below if I assign foreground property in style. I am not sure what is the issue and how to solve it. Any ideas are highly appreciated!
Code when I set foreground directly in the texblock. It is working perfectly
<TextBlock Text="WindowTextColor" Foreground="{Binding WindowTextColor, Source={StaticResource SystemColors}, Converter={StaticResource colorConverter}}" />
Code when I set foreground property through the style. My app crashes:
<UserControl.Resources>
<local:ColorToBrushConverter x:Key="colorConverter" />
<local:SystemColorsWrapper x:Key="SystemColors" />
<Style x:Key="TextBlockStyle1" TargetType="TextBlock">
<Setter Property="Foreground" Value="{Binding WindowColor, Source={StaticResource SystemColors}, Converter={StaticResource colorConverter}}"/>
</Style>
</UserControl.Resources>
<Grid Background="#FFB8B8B8">
<TextBlock Text="WindowColor" Style="{StaticResource TextBlockStyle1}" />
</Grid>
The error I am getting:
System.Windows.Markup.XamlParseException occurred
Message=Set property '' threw an exception. [Line: 11 Position: 41]
LineNumber=11
LinePosition=41
StackTrace:
at System.Windows.Application.LoadComponent(Object component, Uri resourceLocator)
at SilverlightSysColors.MainPage.InitializeComponent()
at SilverlightSysColors.MainPage..ctor()
InnerException: System.NotSupportedException
Message=Cannot set read-only property ''.
StackTrace:
at MS.Internal.XamlMemberInfo.SetValue(Object target, Object value)
at MS.Internal.XamlManagedRuntimeRPInvokes.SetValue(XamlTypeToken inType, XamlQualifiedObject& inObj, XamlPropertyToken inProperty, XamlQualifiedObject& inValue)
InnerException:
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您不能在
Setter
中包含绑定。绑定应用于Setter
的ValueProperty
依赖属性,这并不是您真正的意图。所发生的情况是在 Xaml 解析期间应用样式(在绑定可以应用其值之前),这反过来又导致样式被密封。当绑定尝试调整Setter
的值时,它会失败,因为只有在使用样式后,值才会变为只读。编辑
根据我的猜测,动态绑定实际上并不是那么必要,您只是需要一种方便的方法来访问
SystemColors
静态类的成员。我的方法是创建ResourceDictionary
的派生类,以将一整套资源携带到SystemColors
类,包括每个属性的 Color 和 Brush 并相应命名。一点点反思是有帮助的:-在您的应用程序中使用此类,将其实例包含在 App.Xaml 的
MergedDictionaries
列表中:-现在您可以使用系统颜色属性名称及其“ Brush”变体作为直接静态资源:-
You can't include a binding in the a
Setter
. The binding is applied to theValueProperty
dependency property of theSetter
which isn't really your intention. What happens is the style is applied during Xaml parsing (before the binding can apply its value) which in turn cause the style to be sealed. When an attempt by the binding is made to adjust the value of theSetter
it fails because values become read only once the style has been used.Edit
A work around based on my guess that dynamic binding isn't really that necessary you just want a convenient means of accessing the members of the
SystemColors
static class. My approach would be to create a derivative ofResourceDictionary
to carry a complete set of resources to theSystemColors
class including both a Color and a Brush for each property and named accordingly. A little bit of reflection is helpful:-With this class in your app include an instance of it in your
MergedDictionaries
list in the App.Xaml:-Now you can use the system color property names with their "Brush" variants as straight forward static resources:-