我有一个非常简单的课程
public class SimpleClass // abreviated for this example
{
public bool HighAlert {get;set;}
public bool LowAlert {get;set;}
// about 10 other bools
}
他们都是布尔prorties。如果这样可以使事情变得更容易,我也可以使用结构或可能的数组。
我想将数据绑定到列表框并将“true”的项目突出显示为蓝色或红色。当我从SimpleClass实例化的对象发生更改时,当然会更新列表框。我唯一的另一个要求是在列表框中包含除成员名称之外的内容。例如,拥有“低银行账户警报”而不是“LowAlert”可能会很好。
当然,自动化更多(实际使用SimpleClass的listbox)越多越好,因为有人在SimpleClass中添加了一个属性。
任何示例或教程?我认为这是一个相当常见的情况。
谢谢,
戴夫
答案 0 :(得分:0)
你的问题似乎是双重的。
1)条件格式。您可以在列表框中使用触发器,如下所示:
<Style x:Key="ColoringStyle" TargetType="{x:Type ListBoxItem}">
<Style.Triggers>
<DataTrigger Binding="{Binding Path=HighAlert}" Value="True">
<Setter Property="Background" Value="#33FF0000"></Setter>
</DataTrigger>
<DataTrigger Binding="{Binding Path=LowAlert}" Value="True">
<Setter Property="Background" Value="#33FFDD00"></Setter>
</DataTrigger>
</Style.Triggers>
</Style>
2)以不同方式显示ListBox项目。在WPF中,您可以使用ItemTemplate:
<ListBox.ItemTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal">
--- Have your custom display in here ----
</StackPanel>
</DataTemplate>
</ListBox.ItemTemplate>
两者都被广泛使用,请谷歌任何详细的例子,希望你得到要点