WPF-DataTemplates的参数? [英] WPF - Parameters to DataTemplates?

查看:185
本文介绍了WPF-DataTemplates的参数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个列表框,其中显示有关员工的数据,例如姓名,部门照片,徽章编号等。员工可能具有不同的类型,例如经理,员工,志愿者。我有3个单独的数据模板-每种类型一个。所有这些模板都显示基本相同的数据,但呈现方式却有所不同。

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.

取决于登录到应用程序图片的用户,徽章编号等,是否可见。因此,我对此具有布尔属性-CanSeePhotos,CanSeeBadgeNumbers等。因此,如果CanSeePhotos == false,则所有数据模板都应隐藏照片。

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.

我的问题是,如何我可以在数据模板中使用这些布尔属性来切换适当项目的可见性?当我从TemplateSelector返回参数时,是否可以将参数传递给数据模板?

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?

谢谢!

编辑:按照Ray的想法,我最终这样做了:

edit: following Ray's idea I ended up doing this:

Visibility="{Binding Source={x:Static local:Global.CanSeePhoto}, Converter={StaticResource BooleanToVisibilityConverter}}"


推荐答案

实际上,有一种方法可以自定义DataTemplate,因为它们是通过将它们从TemplateSelector返回而包装

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.

触发器

您可以在DataTemplate内部使用触发器。例如,为在其中显示徽章编号的TextBox或Panel提供一个x:Name,然后在CanSeeBadgeNumebers属性上创建一个DataTrigger。将一个setter添加到DataTrigger中,然后将Visible属性设置为Hidden或Collapsed,并按名称引用。

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.

基本思路:

<DataTemplate>
  ...
  <DockPanel x:Name="BadgeNumberPanel">
    <Label ... />
    <TextBox ... />
  </DockPanel>
  ...

  <DataTemplate.Triggers>
    <DataTrigger Binding="{Binding CanSeeBadgeNumbers}" Value="true">
      <Setter ElementName="BadgeNumberPanel" Property="Visibility" Value="Collapsed" />
    </DataTrigger>
  </DataTemplate.Triggers>
</DataTemplate>

转换器

您可以在代码中创建一个IValueConverter,该代码将 bool类型转换为 Visibility类型(有很多示例可以剪切和粘贴),然后使用转换器将TextBox的可见性绑定到CanSeeBadgeNumbers。 / p>

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.

<DockPanel Visibility="{Binding CanSeeBadgeNumbers, Converter="{x:Static local:BoolToVisibilityConverter.Instance}}">
  <Label ... />
  <TextBox ... />
</DockPanel>

我该怎么做

我实际上对自己的代码使用了不同的技术:我的数据基础包括我编写的MarkupExtension,它调用了我的核心C#表达式解析器,因此我可以说类似

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

Visibility="{edf:Visibility CanSeeBadgeNumber || Owner.SecurityLevel.Count() > 3}"

很遗憾,我的数据基础尚未发布。它是免费和开源的,但是还需要几个月的时间。

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.

这篇关于WPF-DataTemplates的参数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆