有没有办法覆盖列表框中所有项目的颜色?

时间:2016-11-21 20:27:53

标签: c# wpf xaml data-binding

我使用WPF进行GUI设计,我的GUI包含一个列表框。简而言之,我正在尝试使所有列表框的项目具有相同的前景色#FF548E19。我知道输入到列表框中的每个单独项目的前景色可以由XAML属性定义:

Foreground="#FF548E19"

由于我实际上正在使用我构建的视图模型进行数据绑定,因此我无法为每个项目设置Foreground属性,因为它们是在运行时填充的-时间。作为替代方案,我尝试在列表框本身上设置Foreground属性。这没有做任何事情,所有项目仍然显示颜色白色

问题

有没有办法确保每个绑定项(填充到列表框中的项)获得前景色#FF548E19而无需在运行时手动更改颜色?

更新

其中一条评论问我如何用项目填充我的列表框。所以这里是数据绑定属性,它被推送到属性更改的列表框中。

/// <summary>
/// A list of the failure codes (per entry).
/// </summary>
public List<string> FailCodes
{
    get
    {
        return this.failCodes;
    }

    set
    {
        this.failCodes = value;
        this.OnPropertyChanged("FailCodes");
    }
}

此外,这里是XAML属性(包括ItemsSource如何绑定)。

<ListBox x:Name="listBox_failCodes"
         Grid.Row="0" 
         Margin="0" Background="{x:Null}" 
         HorizontalContentAlignment="Center"
         BorderBrush="{x:Null}" 
         Foreground="#FF548E19" 
         ItemsSource="{Binding FailCodes}"/>

1 个答案:

答案 0 :(得分:3)

在您的标记中,使用item template(作为suggested in the comments)并使用静态Foreground值:

<ListBox x:Name="listBox_failCodes"
     Grid.Row="0" 
     Margin="0" Background="{x:Null}" 
     HorizontalContentAlignment="Center"
     BorderBrush="{x:Null}" 
     ItemsSource="{Binding FailCodes}"/>
    <ListBox.ItemTemplate>
        <DataTemplate>
            <TextBlock Foreground="#FF548E19" Text="{Binding}"></TextBlock>
        </DataTemplate>
    </ListBox.ItemTemplate>
</ListBox>

这将适用于绑定到列表框的每个项目

相关问题