设置ListView背景

时间:2014-01-20 08:35:13

标签: wpf wpfdatagrid

我希望用不同的画笔设置背景,但是,它不起作用。没有错误,没有警告。

在我的代码中,我希望ListView中的行具有不同的背景,因此,我为ListViewItem定义了一个Style,但它现在无法知道。当我调试代码时,我可以进入本地:NumberConverter:Convert(对象值,类型targetType,对象参数,CultureInfo文化),然而,行的背景保持其原始颜色。

<UserControl x:Class="demo05_wpf02_binding.TabControl2"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
             xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
             xmlns:local="clr-namespace:demo05_wpf02_binding"
             mc:Ignorable="d" 
             d:DesignHeight="300" d:DesignWidth="300">

    <UserControl.Resources>
        <Style x:Key="BookListStyle" TargetType="{x:Type ListViewItem}">
            <Style.Resources>
                <LinearGradientBrush x:Key="ProfitBrush" StartPoint="0.2,0" EndPoint="0.8,1">
                    <GradientStop Offset="0.3" Color="LavenderBlush" />
                    <GradientStop Offset="0.7" Color="CornflowerBlue" />
                </LinearGradientBrush>

                <LinearGradientBrush x:Key="LossBrush" StartPoint="0.2,0" EndPoint="0.8,1">
                    <GradientStop Offset="0.3" Color="LightYellow" />
                    <GradientStop Offset="0.7" Color="LightGoldenrodYellow" />
                </LinearGradientBrush>

                <local:NumberConverter x:Key="numConverter" />
            </Style.Resources>

            <Style.Triggers>
                <DataTrigger Binding="{Binding Title, Converter={StaticResource numConverter}}" Value="0" >
                    <Setter Property="Background" Value="{StaticResource ProfitBrush}" />
                </DataTrigger>
                <DataTrigger Binding="{Binding Title, Converter={StaticResource numConverter}}" Value="1" >
                    <Setter Property="Background" Value="{StaticResource LossBrush}" />
                </DataTrigger>
            </Style.Triggers>

        </Style>
    </UserControl.Resources>

    <DockPanel>
        <ListView Name="ListView01" ItemContainerStyle="{StaticResource BookListStyle}"  ItemsSource="{Binding}"  Margin="5">
            <ListView.View>
                <GridView>
                    <GridViewColumn Header="Title" DisplayMemberBinding="{Binding Title}" Width="80" />
                    <GridViewColumn Header="Publisher" DisplayMemberBinding="{Binding Publisher}" Width="100" />
                    <GridViewColumn Header="ISBN" DisplayMemberBinding="{Binding ISBN}" Width="100" />
                </GridView>
            </ListView.View>
        </ListView>
    </DockPanel>
</UserControl>

1 个答案:

答案 0 :(得分:0)

您的问题必须在您的Converter中。如果您删除了Converter,那么如果您的数据项的Title10 ,那么您的XAML就可以正常运行

<Style x:Key="BookListStyle" TargetType="{x:Type ListViewItem}">
    <Style.Resources>
        <LinearGradientBrush x:Key="ProfitBrush" StartPoint="0.2,0" EndPoint="0.8,1">
            <GradientStop Offset="0.3" Color="LavenderBlush" />
            <GradientStop Offset="0.7" Color="CornflowerBlue" />
        </LinearGradientBrush>
        <LinearGradientBrush x:Key="LossBrush" StartPoint="0.2,0" EndPoint="0.8,1">
            <GradientStop Offset="0.3" Color="LightYellow" />
            <GradientStop Offset="0.7" Color="LightGoldenrodYellow" />
        </LinearGradientBrush>
    </Style.Resources>            
    <Style.Triggers>
        <DataTrigger Binding="{Binding Title}" Value="0" >
            <Setter Property="Background" Value="{StaticResource ProfitBrush}" />
        </DataTrigger>
        <DataTrigger Binding="{Binding Title}" Value="1" >
            <Setter Property="Background" Value="{StaticResource LossBrush}" />
        </DataTrigger>
    </Style.Triggers>            
</Style>

仔细查看Converter中的代码...在那里放置断点并逐步执行代码,直到找到问题为止。还有一点需要注意的是,你的LossBrush很难被看到......即使只是在测试时使用,颜色颜色也会更强。