使用转换器绑定到静态资源
我有一个 DataGrid
和两个 StaticResource
。
我想将 DataGrid 的 RowStyle
绑定到两个 StaticResources 之一。
RowStyle="{StaticResource {Binding Status, Converter={StaticResource MyConverter}}}"
MyConverter 返回 StaticResource 的 Key。
但我收到此错误:
尝试读取或写入受保护的内存。这通常表明其他内存已损坏。
I have a DataGrid
and two StaticResource
.
I want to bind RowStyle
of DataGrid to one of two StaticResources.
RowStyle="{StaticResource {Binding Status, Converter={StaticResource MyConverter}}}"
MyConverter returns StaticResource's Key.
But I get this error:
Attempted to read or write protected memory. This is often an indication that other memory is corrupt.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
静态资源键不是可以动态分配的值。键的名称需要内联到 Xaml 中。
正确的方法是这样的: -
针对“MyConverter”键存储的转换器返回一个
Style
对象。请注意,您可以向转换器添加ResourceDictionary
类型的属性,并将样式放置在该字典中以供转换器查找。事实上,我已经编写了一个能够实现此功能的转换器
The Static Resource key is not a value that can be assigned Dynamically. The name of the key needs to inline in the Xaml.
The correct approach is this:-
Where the converter that is stored against the "MyConverter" key returns a
Style
object. Note you could add a property of typeResourceDictionary
to you converter and place you styles in that dictionary for you converter to lookup.In fact I have already written a converter capable of this here.