c#listbox:更改文本的前景色

时间:2014-01-17 12:56:11

标签: c# wpf

如何在代码

中的if(InLine)块中更改消息的颜色
public void showMessage(string message, bool InLine)
{
   if (InLine)
      messageBox.Items[messageBox.Items.Count-1] += message;
   else
   {
     ListBoxItem item = new ListBoxItem() { Content = message, Foreground = new SolidColorBrush(Colors.Red) };
     messageBox.Items.Add(item);
   }

}

1 个答案:

答案 0 :(得分:2)

最好在Xaml中使用动画:

<ListBox x:Name="messageBox">
    <ListBox.ItemContainerStyle >
        <Style TargetType="ListBoxItem" >
                     <Style.Triggers>
                <EventTrigger RoutedEvent="Loaded" >
                    <BeginStoryboard>
                        <Storyboard >
                            <ColorAnimation  Storyboard.TargetProperty="Background.Color" From="Red" To="Transparent" />
                        </Storyboard>
                    </BeginStoryboard>
                </EventTrigger>
            </Style.Triggers>
        </Style>
    </ListBox.ItemContainerStyle>
</ListBox>