我有一个像tcp应用程序的聊天。我想区分发送和接收消息的颜色。我该如何以编程方式执行此操作? 任何帮助表示赞赏
修改 如果我可以改变行颜色就没问题。
答案 0 :(得分:1)
您可以更改项目的文本前景。
item.foreground = new SolidColorBrush(Colors.Green);
答案 1 :(得分:1)
好的,这样做..
创建一个类
public class MSGS
{
public string color {get;set;}
public string message {get;set;}
}
现在而不是在List<String>
创建List<MSGS>
中添加项目,并将消息设置为等于消息,如果发送消息,则设置颜色让我们说Blue
或者如果收到消息则设置颜色为Red
。
MSGS one = new MSGS ();
one.message = "testing";
one.color = "Red";
MSGS two = new MSGS();
two.message = "testing2";
two.color = "Blue";
MSGS three = new MSGS();
three.message = "testing3";
three.color = "Red";
List<MSGS> list = new List<MSGS> ();
list.Add(one);
list.Add(two);
list.Add(three);
myLB.ItemsSource = list;
在XAML中为listboxitem定义一个样式
<UserControl.Resources>
<Style TargetType="ListBoxItem">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate>
<TextBlock Foreground="{Binding Path=color}" Text="{Binding Path=message}"/>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</UserControl.Resources>
此代码将显示收到的消息并以不同的颜色发送消息
更详细的方法 尝试下面的样式而不是上面的样式
<UserControl.Resources>
<DataTemplate x:Key="DataTemplate1">
<StackPanel>
<TextBlock Text="{Binding message}"/>
</StackPanel>
</DataTemplate>
<DataTemplate x:Key="DataTemplate2">
<StackPanel>
<TextBlock Text="{Binding message}" FontWeight="Bold"/>
</StackPanel>
</DataTemplate>
<Style TargetType="ListBoxItem">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="ListBoxItem">
<Grid Background="{Binding Path=type}">
<VisualStateManager.VisualStateGroups>
<VisualStateGroup x:Name="CommonStates">
<VisualState x:Name="Normal"/>
<VisualState x:Name="MouseOver">
<Storyboard>
<DoubleAnimation Duration="0" To=".35" Storyboard.TargetProperty="Opacity" Storyboard.TargetName="fillColor"/>
</Storyboard>
</VisualState>
<VisualState x:Name="Disabled">
<Storyboard>
<DoubleAnimation Duration="0" To=".55" Storyboard.TargetProperty="Opacity" Storyboard.TargetName="contentPresenter1"/>
</Storyboard>
</VisualState>
</VisualStateGroup>
<VisualStateGroup x:Name="SelectionStates">
<VisualState x:Name="Unselected"/>
<VisualState x:Name="Selected">
<Storyboard>
<DoubleAnimation Duration="0" To=".75" Storyboard.TargetProperty="Opacity" Storyboard.TargetName="fillColor2"/>
<ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="(UIElement.Visibility)" Storyboard.TargetName="contentPresenter1">
<DiscreteObjectKeyFrame KeyTime="0">
<DiscreteObjectKeyFrame.Value>
<Visibility>Collapsed</Visibility>
</DiscreteObjectKeyFrame.Value>
</DiscreteObjectKeyFrame>
</ObjectAnimationUsingKeyFrames>
<ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="(UIElement.Visibility)" Storyboard.TargetName="contentPresenter2">
<DiscreteObjectKeyFrame KeyTime="0">
<DiscreteObjectKeyFrame.Value>
<Visibility>Visible</Visibility>
</DiscreteObjectKeyFrame.Value>
</DiscreteObjectKeyFrame>
</ObjectAnimationUsingKeyFrames>
</Storyboard>
</VisualState>
</VisualStateGroup>
<VisualStateGroup x:Name="FocusStates">
<VisualState x:Name="Focused">
<Storyboard>
<ObjectAnimationUsingKeyFrames Duration="0" Storyboard.TargetProperty="Visibility" Storyboard.TargetName="FocusVisualElement">
<DiscreteObjectKeyFrame KeyTime="0">
<DiscreteObjectKeyFrame.Value>
<Visibility>Visible</Visibility>
</DiscreteObjectKeyFrame.Value>
</DiscreteObjectKeyFrame>
</ObjectAnimationUsingKeyFrames>
</Storyboard>
</VisualState>
<VisualState x:Name="Unfocused"/>
</VisualStateGroup>
</VisualStateManager.VisualStateGroups>
<Rectangle x:Name="fillColor" Fill="#FFBADDE9" IsHitTestVisible="False" Opacity="0" RadiusY="1" RadiusX="1"/>
<Rectangle x:Name="fillColor2" Fill="#FFBADDE9" IsHitTestVisible="False" Opacity="0" RadiusY="1" RadiusX="1"/>
<ContentPresenter x:Name="contentPresenter1" ContentTemplate="{StaticResource DataTemplate1}" Content="{TemplateBinding Content}" HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}" Margin="{TemplateBinding Padding}"/>
<ContentPresenter x:Name="contentPresenter2" ContentTemplate="{StaticResource DataTemplate2}" Content="{TemplateBinding Content}" HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}" Margin="3,3,0,3" Visibility="Collapsed"/>
<Rectangle x:Name="FocusVisualElement" RadiusY="1" RadiusX="1" Stroke="#FF6DBDD1" StrokeThickness="1" Visibility="Collapsed"/>
<ListBox HorizontalAlignment="Left" Height="4" Margin="-90,0,0,-163" VerticalAlignment="Bottom" Width="31"/>
</Grid>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</UserControl.Resources>
从http://forums.silverlight.net/forums/p/35969/113333.aspx
获取此信息如果您想了解更多关于样式的信息,请查看这些链接
http://www.silverlightshow.net/items/Skinning-and-Styling-Silverlight-Controls.aspx
答案 2 :(得分:1)
我用一个使用样式转换器的绑定做了一个例子。
您可以在此处阅读整个实施和示例:http://vanderbiest.org/blog/2011/07/12/listbox-individual-item-color-in-silverlight/
列表框实施
<listbox itemssource="{Binding Messages}" maxwidth="300" borderbrush="Black" borderthickness="2">
<listbox.itemtemplate>
<datatemplate>
<textblock text="{Binding Name}" style="{Binding IsInError, Converter={StaticResource listboxMessageStyleConverter}}">
</textblock></datatemplate>
</listbox.itemtemplate>
</listbox>
转换器实施
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
if ((bool)value)
{
return Application.Current.Resources["ListBoxTextNormalItem"];
}
return Application.Current.Resources["ListBoxTextErrorItem"];
}
样式实施
<style x:key="ListBoxTextNormalItem" targettype="TextBlock">
<Setter Property="Foreground">
<Setter.Value>
<SolidColorBrush Color='#0A7B27' />
</Setter.Value>
</Setter>
</style>
<style x:key="ListBoxTextErrorItem" targettype="TextBlock">
<Setter Property="Foreground">
<Setter.Value>
<SolidColorBrush Color='#FF2A1B' />
</Setter.Value>
</Setter>
<Setter Property="FontWeight" Value="Bold" />
</style>
答案 3 :(得分:0)
查看以下链接; http://forums.create.msdn.com/forums/p/74900/455850.aspx 它具有改变所选项目的前景色的工作解决方案。因此,当您收到消息时,将其标记为具有样式的选定项目,您可以更改文本的前景色。