简体   繁体   English

如何从后面的代码更改列表框项目字体颜色

[英]how to change listBox item font colour from code behind

I have tried to access ListBox items properties like this:我试图访问这样的 ListBox 项目属性:

mylistBox.Items[0] 

and then to set foreground property but for unknow to me reason i cant access it this way.然后设置前景属性,但由于我不知道的原因,我无法以这种方式访问​​它。 Can i even do this using code behind - and if yes - how?我什至可以使用背后的代码来做到这一点 - 如果是的话 - 怎么做?

Implement ItemTemplate and bind your Brush to Foreground property.实现 ItemTemplate 并将您的 Brush 绑定到 Foreground 属性。

You should implement INotifyPropertyChanged in your binding object and notify on MyColorBrush value change.您应该在绑定对象中实现 INotifyPropertyChanged 并通知 MyColorBrush 值更改。

<ListBox ItemsSource="{Binding MyItems}">
    <ListBox.ItemTemplate>
        <DataTemplate>
            <TextBlock Text="{Binding MyText}" Foreground="{Binding MyColorBrush}"/>
        </DataTemplate>
    </ListBox.ItemTemplate>
</ListBox>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM