我知道在FrameworkElement(例如Window)上调用FindResource()可以用来查找FrameworkElement的ResourceDictionary中的资源。
例如,我经常使用它通过代码访问Style以动态地向Style添加新的Setter。我总是将Style的x:Key值作为字符串传递给FindResource()方法。像这样...
有人知道为什么FindResource()接受对象而不是字符串类型的参数吗?如果知道,请举一个使用非字符串类型参数调用FindResource()的例子?
谢谢。
例如,我经常使用它通过代码访问Style以动态地向Style添加新的Setter。我总是将Style的x:Key值作为字符串传递给FindResource()方法。像这样...
Style style = w.FindResource("GridDescriptionColumn") as Style;
我的问题是,我注意到FindResource()接受类型为object的参数,而不是类型为string的参数。我无法想象我会以不是字符串的参数调用FindResource()的原因。这让我想到我可能不知道其他使用FindResource()的方法。有人知道为什么FindResource()接受对象而不是字符串类型的参数吗?如果知道,请举一个使用非字符串类型参数调用FindResource()的例子?
谢谢。
FindResource(typeof(Button))
did not work for me when working on a TreeView with DataTemplates in the TreeView.Resources, I had to wrap the type object in a key class -element.FindResource(new DataTemplateKey(typeof(MyClass)))
- bakerhillpins