如何绑定字典项属性?
我想做这样的事情:
<Controls:TreeView>
<Controls:TreeViewItem Header="Persons"
Visibility="{Binding Items[Persons], Path=IsVisible, Converter={StaticResource toVisiblityConverter}}"/>
...
</Controls:TreeView>
(TreeView DataContext 绑定到包含字典的 ViewModel。并且字典项的值具有属性 IsVisible)。
这不起作用:多次设置路径属性。我怎样才能避免这种情况?
I want to make something like this:
<Controls:TreeView>
<Controls:TreeViewItem Header="Persons"
Visibility="{Binding Items[Persons], Path=IsVisible, Converter={StaticResource toVisiblityConverter}}"/>
...
</Controls:TreeView>
(TreeView DataContext bind to the ViewModel that contain dictionary. And value of the dictionary item have property IsVisible).
This doesn't work: path property setted more than once. How can I avoid this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我想您想根据特定键中的 IsVisible 值隐藏或显示多个树项目。实现这一目标的方法很少。
您可以保留模板的默认树部分,并为每列生成一个
TreeViewItem
。你可以这样做:您可以创建项目模板。它是为数据源中的每个项目生成的模板。在该模板中,您应该可以轻松访问字典中的一项。
另一种可行的方法是编写另一个转换器,它将接受整个字典对象和项目名称作为参数。然后它可以获取 C# 代码中的值并返回可见性。通过这种方式,您可以显式定义要获取其值的列。然后你可以像这样使用它:
I suppose you want to hide or show multiple tree items based on IsVisible value in the specific key. There are few methods to achieve that.
You can preserve the default tree part of the template and generate a
TreeViewItem
for each column. You can do it like this:You can create item template. It's a template that is generated for each item in the data source. In that template you should have an easy access to one item from the dictionary.
Another method that may work, would be writing another converter that would accept the whole dictionary object and item name as a parameter. Then it could get the value in the c# code and return the visibility. This way you can explicitly define column you want to get the value for. Then you could use it like this: