WPF 绑定:在绑定路径中进行转换
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16560550/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
WPF Binding : Casting in binding path
提问by Aminouvic
I've a binding where the Pathis set to Path=Item.Tag.caption, but I need to cast Itemto IEDGEfirst so I can access the TagProperty.
Is there a way to achieve this?
我有一个结合,其中的Path设置为Path=Item.Tag.caption,但我需要投Item以IEDGE第一,所以我可以访问Tag属性。有没有办法实现这一目标?
回答by Aminouvic
The solution for the problem, finally, is to use following syntax:
最后,该问题的解决方案是使用以下语法:
Path=Item.(myNameSpace:IEdge.Tag).caption
The previous code cast Itemto the type IEdgein order to access the Tagproperty.
前面的代码强制Item转换为类型IEdge以访问该Tag属性。
In case of multiple nested casts the global pattern is :
在多个嵌套强制转换的情况下,全局模式是:
Path=Obj1.(ns1:TypeObj1.Obj2).(ns2:TypeObj2.Obj3)...(nsN:TypeObjN.BindedProp)
As suggested in comments Do not use shorthand binding syntax when using this solution. Ensure you actually use Path=otherwise it won't work!
正如评论中所建议的,在使用此解决方案时不要使用速记绑定语法。确保您实际使用,Path=否则将无法使用!

