ItemTemplate TextBlock 的问题 [英] Problem with ItemTemplate TextBlock

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

问题描述

我正在尝试制作一个项目模板,其中我的堆栈面板中的某些字段可以为空.当它为空时,我想将可见性设置为折叠.我尝试放置触发器,但它似乎不起作用,而且我对 WPF 的这一部分不是很熟悉

i am trying to make an item template where some of the field in my stack panel can be empty. When it's empty, I would like to set the visiblility to collapsed. I tried putting triggers but it doesn't seem to work and I am not very familiar with this part of WPF

此外,当我的绑定中的特定值为真时,我想更改此项目的背景颜色.是一样的吗?

Also, I would like to change the color of the background of this item when a specific value in my binding is true. Is it the same thing?

谢谢.

推荐答案

使用 ViewModel 是解决此类问题的一种方法.

Using a ViewModel is one approach to solving this kind of problem.

如果您的数据存储在 Item 类中,您将创建一个 ItemViewModel 来包装 Item 以在您的项目控件中显示.ViewModel 类将实现 INotifyProperty 更改以更新显示,并且设置器将引发传递适当属性名称的 PropertyChanged 事件.您还可以根据需要为尽可能多的相关更改字段引发属性更改事件.

The if your data was stored in an Item class you would make an ItemViewModel to wrap the Item for display in your items control. The ViewModel class would implement INotifyProperty changed in order to update the display and the setters would raise the PropertyChanged event passing the appropriate property name. You can also raise property changed events for as many interrelated changed fields as necessary.

假设您希望 Item.Description 在 Description 为空时显示在折叠字段中.您的 ViewModel 属性可能如下所示

Suppose you wanted Item.Description to display in a collapsed field when Description is empty. Your ViewModel properties could look like this

public string Description
{
    get { return mItem.Description; }
    set { mItem.Description = value; Notify("Description"); Notify("DescriptionVisibility"); }
}

public Visibility DescriptionVisibility
{
    get { return string.IsNullOrEmpty(mItem.Description) ? Visibility.Visible : Visibility.Collapsed; }
}

在 XAML 中,将 text 属性绑定到 Description,将 Visibility 属性绑定到 DescriptionVisibility.

In the XAML bind the text property to Description and the Visibility property to DescriptionVisibility.

这篇关于ItemTemplate TextBlock 的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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