列表视图项的替代颜色
Alternative color for listview items
我的xaml文件中有下面的列表视图。它绑定了一个平台集合。其中包含警告和错误消息。文本块前景色设置为红色。我需要为不同的消息设置不同的颜色。如何为各种消息设置不同的颜色?
<ListView x:Name="mylist" Width="578" ScrollViewer.VerticalScrollMode="Enabled" ScrollViewer.HorizontalScrollBarVisibility="Disabled" Background="White" Grid.Row="2" Grid.ColumnSpan="3" ItemsSource="{x:Bind Errors}">
<ListView.ItemTemplate>
<DataTemplate>
<StackPanel Orientation="Vertical" ScrollViewer.VerticalScrollBarVisibility="Auto">
<TextBlock Foreground="Red" TextWrapping="Wrap" Text="{Binding}" />
</StackPanel>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
请指教。
文本块前景色设置为红色。我需要为不同的消息设置不同的颜色。
您可以使用IValueConverter
接口来处理,使用转换器类将不同的消息类型转换为不同的SolidBrush
用于文本块Foreground
。您可以参考以下颜色转换器。
public class ColorConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, string language)
{
if (Boolean.Parse(value.ToString()))
{
return new SolidColorBrush(Colors.Green);
}
else
{
return new SolidColorBrush(Colors.Gray);
}
}
public object ConvertBack(object value, Type targetType, object parameter, string language)
{
throw new NotImplementedException();
}
}
用法
<Page.Resources>
<local:ColorConverter x:Key="Converter"/>
</Page.Resources>
......
<TextBlock Text="{Binding info}" Foreground="{Binding messageType,Converter={StaticResource Converter}}"/>
相关文章:
- 如何在选项卡视图Qt中设置一个新项目,并保存以前的项目
- 视图中的参数推导失败:take_while
- 将"打开的CV图像"中的"颜色"转换为整数格式
- 如何在内核C++中使用1920x1080x16M图形或类似的16M颜色?(VGA)
- 如何在24位SDL_Surface上设置像素的颜色
- 如何维护资源管理器项目视图中当前可见的项目列表
- 在另一个类视图中添加最多2个图表的正确方法是什么
- 如何从SDL_Surface获取特定像素的颜色
- OpenVR:向视图方向移动
- 使用 WIN32 API (C/C++) 对特定树视图项进行着色
- 将所选值(通过视图)从 boost::multi_array 复制到另一个数组 (C++)
- 如何在不使用滚动条的情况下使视图更改
- 列表视图更改选择颜色
- GtkTreeView 交替行颜色
- dx11 渲染到纹理仅显示透明颜色
- Qt - QVector 和模型视图 - 从列表视图获取自定义类的最佳方法是什么?
- 列表视图项的替代颜色
- 如何更改列表视图的边框颜色
- 自定义图形列表视图项目不更改文本颜色
- Qt5 Qml改变表视图的高亮颜色