如何公开 WPF DataTemplate 的属性?

发布于 2024-07-16 06:27:09 字数 295 浏览 4 评论 0原文

我有一个在很多页面中使用的数据模板,该数据模板包含一些按钮,我想通过触发器隐藏其中一些按钮(我的意思是在我使用此 DataTemplate 的页面中设置这些按钮的 IsEnabled 属性)。

换句话说,我什至想在样式触发器/设置器中设置属性“ButtonXIsEnabled”、“ButtonYIsEnabled”作为我使用此 DataTemplate 的 ListBox 中 DataTemplate 可设置的一部分。

我真的希望我说得足够清楚,请留下评论以了解更多细节。

任何讨论将不胜感激! 提前致谢。

I have a data template that I use in many pages, the data template contains a few buttons, I want to hide some of these buttons by triggers (I mean setting the IsEnabled Property of these buttons in the page where I use this DataTemplate).

In other words, I would even like to set in style triggers/setters a property 'ButtonXIsEnabled', 'ButtonYIsEnabled' as part of the DataTemplate settable from the ListBox where I use this DataTemplate.

I really hope I am clear enough, please leave comments for any further details.

Any discussion will be really appreciated!
Thanks in advance.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

阳光下的泡沫是彩色的 2024-07-23 06:27:09

基本上这取决于您的数据模板使用的对象。 而不是使用一些 ButtonYIsEnabled 等。 尝试使用一些更适合您的领域模型的单词。

例如,假设您有一个客户列表,其中一些客户有能力购买折扣产品。 然后向您的客户添加一个名为 CanPurchaseDiscountedProducts 的属性,并在您的 DataTemplate 中使用该属性

<DataTemplate TargetType="{x:Type local:Customer}">
  <!-- Other Items -->
  <Button Content="Purchase Discounted Products" x:Name="discounts" Visibility="Hidden" />
  <DataTemplate.Triggers>
    <DataTrigger Binding="{Binding CanPurchaseDiscountedProducts}" Value="True">
      <Setter TargetName="discounts" Property="Visibility" Value="Visible"/>
    </DataTrigger>
  </DataTemplate.Triggers>
</DataTemplate>

Basically this depends on what object your using for your datatemplate. Instead of using some ButtonYIsEnabled, etcs. Try to use some words that fit better in to your domain model.

For example say you have a list of customers, and some of those customers have the ability to purchase discounted products. Then add a property to your Customer called CanPurchaseDiscountedProducts, and use that property in your DataTemplate

<DataTemplate TargetType="{x:Type local:Customer}">
  <!-- Other Items -->
  <Button Content="Purchase Discounted Products" x:Name="discounts" Visibility="Hidden" />
  <DataTemplate.Triggers>
    <DataTrigger Binding="{Binding CanPurchaseDiscountedProducts}" Value="True">
      <Setter TargetName="discounts" Property="Visibility" Value="Visible"/>
    </DataTrigger>
  </DataTemplate.Triggers>
</DataTemplate>
淡墨 2024-07-23 06:27:09

WPF 数据模板是某种对象类型的视图...您希望 ObjectTypeX 实例的外观如何。 数据模板可以绑定到底层实例上的属性。

因此,如果实例上有 ButtonXIsEnabled 属性,则可以将相应 Button 的 Visibility 属性绑定到实例属性。 该按钮将根据基础对象中的值显示或隐藏。

A WPF data template is a view of a certain object type... how you want an instance of ObjectTypeX to look. The data template can bind to properties on the underlying instance.

So if you have a ButtonXIsEnabled property on your instance, you can bind the corresponding Button's Visibility property to the instance property. The button would be shown or hidden based on the value in the underlying object.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文