请问 WPF中如何动态改变 ListView中 某一个值 颜色。
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了请问 WPF中如何动态改变 ListView中 某一个值 颜色。相关的知识,希望对你有一定的参考价值。
ListView已经绑定数据库,希望能在数据库某个值改变,修改该值对应的 ListView控件中那一个位置的颜色。
wpf和silverlight一样,在数据绑定的时候提供了前台显示转换机制——Converter。比如你一个textblock显示一个int值,当值>0显示绿色;小于0显示红色。
那么你创建一个Converter类做下数据转换的处理(网上有很多实例及相关教程)
前台就是这样使用:<textblock text= binding 内容属性 foreGround = binding 内容属性,Converter=staicResource 创建的Converter资源>
这样一来 textblock的字体颜色就会随内容变化而变化。同理可应用到ListView控件。追问
下班了 明天就来试试 先感谢下 ~~明天可能还会继续拜托您的 ~
参考技术A 绑定数据库后..遍历下ListView,通过验证某个值是否发生改变从而改变颜色WPF中ListView如何改变选中条背景颜色
先上图
解决方法:
<ListView ...> <ListView.ItemContainerStyle> <Style TargetType="{x:Type ListViewItem}"> <Style.Resources> <!-- Foreground for Selected ListViewItem --> <SolidColorBrush x:Key="{x:Static SystemColors.HighlightTextBrushKey}" Color="Black"/> <!-- Background for Selected ListViewItem --> <SolidColorBrush x:Key="{x:Static SystemColors.HighlightBrushKey}" Color="Transparent"/> </Style.Resources> </Style> </ListView.ItemContainerStyle> ... </ListView>
解决问题
在CSDN上找到另外一种方法:
用StyleSnooper看一下默认的Style,改一下就可以了。 三种颜色 IsMouseOver = Blue Select & Focus = Red Select & UnFocus = Yellow
大家也可以尝试下:
<Window x:Class="WpfApplication1.Window1" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Title="Window1" Height="300" Width="300"> <Window.Resources> <Style TargetType="ListViewItem"> <Style.Resources> <SolidColorBrush x:Key="{x:Static SystemColors.ControlBrushKey}" Color="Yellow"/> <SolidColorBrush x:Key="{x:Static SystemColors.HighlightBrushKey}" Color=" Red"/> </Style.Resources> <Setter Property="Panel.Background" Value="#00FFFFFF"/> <Setter Property="Control.HorizontalContentAlignment"> <Setter.Value> <Binding Path="HorizontalContentAlignment" RelativeSource="{RelativeSource Mode=FindAncestor, AncestorType=ItemsControl, AncestorLevel=1}" /> </Setter.Value> </Setter> <Setter Property="Control.VerticalContentAlignment"> <Setter.Value> <Binding Path="VerticalContentAlignment" RelativeSource="{RelativeSource Mode=FindAncestor, AncestorType=ItemsControl, AncestorLevel=1}" /> </Setter.Value> </Setter> <Setter Property="Control.Padding" Value="2,0,0,0"/> <Setter Property="Control.Template"> <Setter.Value> <ControlTemplate TargetType="ListViewItem"> <Border BorderThickness="{TemplateBinding Border.BorderThickness}" Padding="{TemplateBinding Control.Padding}" BorderBrush="{TemplateBinding Border.BorderBrush}" Background="{TemplateBinding Panel.Background}" Name="Bd" SnapsToDevicePixels="True"> <ContentPresenter Content="{TemplateBinding ContentControl.Content}" ContentTemplate="{TemplateBinding ContentControl.ContentTemplate}" ContentStringFormat="{TemplateBinding ContentControl.ContentStringFormat}" HorizontalAlignment="{TemplateBinding Control.HorizontalContentAlignment}" VerticalAlignment="{TemplateBinding Control.VerticalContentAlignment}" SnapsToDevicePixels="{TemplateBinding UIElement.SnapsToDevicePixels}" /> </Border> <ControlTemplate.Triggers> <Trigger Property="Selector.IsSelected" Value="True"> <Setter Property="Panel.Background" TargetName="Bd"> <Setter.Value> <DynamicResource ResourceKey="{x:Static SystemColors.HighlightBrushKey}" /> </Setter.Value> </Setter> <Setter Property="TextElement.Foreground"> <Setter.Value> <DynamicResource ResourceKey="{x:Static SystemColors.HighlightTextBrushKey}" /> </Setter.Value> </Setter> </Trigger> <MultiTrigger> <MultiTrigger.Conditions> <Condition Property="Selector.IsSelected" Value="True"/> <Condition Property="Selector.IsSelectionActive" Value="False"/> </MultiTrigger.Conditions> <Setter Property="Panel.Background" TargetName="Bd"> <Setter.Value> <DynamicResource ResourceKey="{x:Static SystemColors.ControlBrushKey}" /> </Setter.Value> </Setter> <Setter Property="TextElement.Foreground"> <Setter.Value> <DynamicResource ResourceKey="{x:Static SystemColors.ControlTextBrushKey}" /> </Setter.Value> </Setter> </MultiTrigger> <Trigger Property="UIElement.IsEnabled" Value="False"> <Setter Property="TextElement.Foreground"> <Setter.Value> <DynamicResource ResourceKey="{x:Static SystemColors.GrayTextBrushKey}" /> </Setter.Value> </Setter> </Trigger> <Trigger Property="UIElement.IsMouseOver" Value="True"> <Setter Property="Panel.Background" TargetName="Bd" Value="Blue"> </Setter> </Trigger> </ControlTemplate.Triggers> </ControlTemplate> </Setter.Value> </Setter> </Style> </Window.Resources> <Grid> <ListView Margin="48,22,110,0" Name="listView1" Height="100" VerticalAlignment="Top"> <ListView.View> <GridView> <GridViewColumn Header="12"/> <GridViewColumn Header="12"/> <GridViewColumn Header="12"/> </GridView> </ListView.View> <ListViewItem>123</ListViewItem> <ListViewItem>123</ListViewItem> <ListViewItem>123</ListViewItem> </ListView> <TextBox Height="23" Margin="94,0,64,67" Name="textBox1" VerticalAlignment="Bottom" /> </Grid> </Window>
以上是关于请问 WPF中如何动态改变 ListView中 某一个值 颜色。的主要内容,如果未能解决你的问题,请参考以下文章