WPF:将转换器应用到 ItemsSource
是否可以将转换器应用于 xaml 中控件的数据源?
或者也许还有另一种方法可以做到这一点。
基本上我有一个接受特定类型对象的自定义控件。该对象与该控件紧密绑定。我不想在我的视图模型中转换为这种类型。因此,我希望能够绑定到常规属性(例如 List),并通过转换器将其自动转换为我的对象。
我尝试过类似的事情。
ItemsSource="{Binding CurrentTables, Converter={x:Static cconverters:SpyFilterDataObjectConverter}}"
is it possible to apply a converter to a data source of a control in xaml?
or perhaps there is another way to do this.
Basically i have a custom control that accepts a specific type of object. that object is tightly bound to that control. I don't want to convert to this type all over my view model. So i would like to be able to bind to regular properties such as List and have it automatically translated to my object by a converter.
I've attempted something like this.
ItemsSource="{Binding CurrentTables, Converter={x:Static cconverters:SpyFilterDataObjectConverter}}"
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
嗯,对我来说使用这种转换器似乎不太好。基本上,转换器仅执行一次转换操作,因此您不会收到任何更新。我使用了不同的方法 - 只需创建某种包含初始集合(它应该实现 INotifyCollectionChanged)的包装器和一些将初始对象转换为包装对象的包装策略。
Well, it doesn't seem good as for me to use such kind of converters. Basically, converter performs conversion operation only once, so you will not receive any updates. I've used different approach - just create some sort of wrapper that contains an initial collection (it should implement INotifyCollectionChanged) and some wrap strategies that converts your initial object to wrapped one.
x:Static
的语法为namespace:Type.StaticMember
,您应该实例化转换器并将其公开为静态属性。或者,您可以在
App.xaml
的Application.Resources
中创建一个实例,然后您可以使用其密钥将其作为静态资源在整个应用程序中引用。x:Static
has the syntaxnamespace:Type.StaticMember
, you should instantiate the converter and expose it as a static property.Alternatively you can create an instance in the
Application.Resources
in yourApp.xaml
, then you can reference it as a static resource throughout the application using its key.