【发布时间】:2021-10-31 07:59:48
【问题描述】:
我有一个ListBox 的基本设置,其ItemSource 属性设置为ObservableCollection<Human>。
<ListBox ItemsSource="{Humans}" DisplayMemberPath="Name">
<ListBox.ItemContainerStyle>
<Style TargetType="ListBoxItem">
<!-- Some setters -->
</Style>
</ListBox>
Human 是这样定义的:
public class Human
{
public string Name { get; set; }
public bool IsAnswered { get; set; }
public override string ToString() => this.Name;
}
所以我们有一个 Human 对象作为列表框的每个项目的源,并显示其字符串表示形式的默认行为(在本例中为 Name 属性)。
现在,当IsAnswered 更改为true 时,我希望将显示的Human.Name 值设置为粗体。如何做到这一点?
【问题讨论】:
-
通过 ItemContainerStyle 中的 DataTrigger 或 ItemTemplate 中的 Binding。人类必须实现 INotifyPropertyChanged 并在 IsAnswered 设置器中触发 PropertyChanged 事件。
-
但是如何将
DataTrigger中的ItemContainerStyle指向IsAnswered? -
项目容器的 DataContext 始终是数据模型,在您的情况下是 Human 实例。
标签: wpf triggers listbox styles listboxitem