WPF - 数据模板的参数?
我有一个列表框,显示有关员工的数据,例如姓名、部门照片、徽章号码等。员工可能有不同的类型,例如经理、员工、志愿者。我有 3 个独立的数据模板 - 每种类型一个。所有这些模板显示的数据基本相同,但呈现方式不同。
根据登录应用程序的用户,图片、徽章编号等可以或不可见。所以我有布尔属性 - CanSeePhotos、CanSeeBadgeNumbers 等。因此,如果 CanSeePhotos == false,则所有数据模板都应隐藏照片。
我的问题是,如何在数据模板中使用这些布尔属性来切换相应项目的可见性?当我从 TemplateSelector 返回参数时,有没有办法将参数传递给数据模板?
谢谢!
编辑:按照雷的想法我最终这样做了:
Visibility="{Binding Source={x:Static local:Global.CanSeePhoto}, Converter={StaticResource BooleanToVisibilityConverter}}"
I have a ListBox showing data about employees such as name, department photo, badge number, etc. Employees may have different types such as Manager, Staff, Volunteers. I have 3 separate data templates - one for each type. All of these templates show basically the same data but presented differently.
Depending on the user logged into the application pictures, Badge Number, etc, can or cannot be visible. So I have boolean properties for that - CanSeePhotos, CanSeeBadgeNumbers, etc. So if CanSeePhotos == false, all data templates should hide the photos.
My question is, how can I use these boolean properties inside my data templates to toggle the visibility of the appropriate items? Is there a way to pass parameters to Data Templates as I return them from my TemplateSelector?
Thanks!
edit: following Ray's idea I ended up doing this:
Visibility="{Binding Source={x:Static local:Global.CanSeePhoto}, Converter={StaticResource BooleanToVisibilityConverter}}"
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
实际上,有一种方法可以自定义 DataTemplates,因为它们是从 TemplateSelector 返回的,方法是将它们包装在 FrameworkElementFactory 中,但它对于您的需求来说太复杂了。
对于您的情况,有两种更简单的解决方案:触发器和转换器。
触发器
您可以在 DataTemplate 内使用触发器。例如,为显示徽章编号的文本框或面板指定 x:Name,然后在 CanSeeBadgeNumberers 属性上创建 DataTrigger。向 DataTrigger 添加一个 setter,并将 Visible 属性设置为 Hidden 或 Collapsed,并按名称引用它。
基本思想:
转换器
您可以在代码中创建一个 IValueConverter,将“bool”类型转换为“Visibility”类型(有很多示例可以剪切和粘贴),然后绑定 TextBox 的可见性使用转换器转换为 CanSeeBadgeNumbers。
如何实现
我实际上对自己的代码使用了不同的技术:我的数据基础包括我编写的 MarkupExtension,它调用我的核心 C# 表达式解析器,因此我可以说类似“
不幸的是,我的数据基础没有”尚未被释放。当它到来时,我计划将其免费和开源,但这还需要几个月的时间。
Actually there is a way to customize DataTemplates as they are returned from the TemplateSelector by wrapping them inside a FrameworkElementFactory, but it is much too complex for your needs.
For your case there are two solutions that are much easier: triggers and converters.
Triggers
You can use a trigger inside the DataTemplate. For example, give the TextBox or Panel where you display the badge number an x:Name, then create a DataTrigger on the CanSeeBadgeNumebers property. Add one setter to the DataTrigger and set the Visible property to Hidden or Collapsed, referencing it by name.
Basic idea:
Converters
You can create an IValueConverter in code that converts "bool" type to "Visibility" type (there are many examples out there you can cut and paste), then bind the visibilty of the TextBox to the CanSeeBadgeNumbers, using the converter.
How I Do It
I actually use a different technique for my own code: My data foundation includes a MarkupExtension I wrote that calls my core C# expression parser, so I can say something like
Unfortunately my data foundation has not yet been released. When it is, I plan to make it free and open source, but that's a few months away yet.