0

我有三个数据数组,它们将填充ListView一个DisplayMemberBinding

我可以向绑定项添加逻辑以更改ListView单元格的前景或背景属性吗?

xml:

<Grid>

    <ListView x:Name="carList" VerticalAlignment="Top">
        <ListView.View>
            <GridView>
                <GridViewColumn Header="Car" DisplayMemberBinding="{Binding Name}" Width="Auto"/>
                <GridViewColumn DisplayMemberBinding="{Binding Make}" Header="Make" Width="Auto"/>
                <GridViewColumn DisplayMemberBinding="{Binding Year}" Header="Year" Width="Auto"/>
            </GridView>
        </ListView.View>
    </ListView>

    <Button x:Name="generate" Content="Parse" Click="generate_Click" Margin="0,5,5,5" DockPanel.Dock="Top" VerticalAlignment="Bottom" HorizontalAlignment="Right"/>

</Grid>

CS:

public MainWindow()
{
    InitializeComponent();
}

public class Car
{
    public string Name { get; set; }
    public string Make { get; set; }
    public string Year { get; set; }
}

private void generate_Click(object sender, RoutedEventArgs e)
{
    int i = 0;
    string[] name = { "Sentra", "IS", "Camry" };
    string[] make = { "Nissan", "Lexus", "Toyota" };
    string[] year = { "2003", "2011", "2013" };

    foreach (string s in name)
    {
        Car car = new Car();
        car.Name = name[i];
        car.Make = make[i];
        car.Year = year[i];

        if (year[i] == "2013")
        {
            //Do something here
            //Make bold, change color, etc.
        }

        carList.Items.Add(car);

        i++;
    }
}
4

1 回答 1

1

您可以在 XAML 中执行此操作:

<ListView x:Name="carList" VerticalAlignment="Top">                
    <ListView.View>
        <GridView>
            <GridViewColumn Header="Car" DisplayMemberBinding="{Binding Name}" Width="Auto"/>
            <GridViewColumn DisplayMemberBinding="{Binding Make}" Header="Make" Width="Auto"/>
            <GridViewColumn Header="Year" Width="Auto">
                <GridViewColumn.CellTemplate>
                    <DataTemplate>
                        <TextBlock Text="{Binding Year}">
                            <TextBlock.Style>
                                <Style TargetType="TextBlock">
                                    <Style.Triggers>
                                        <DataTrigger Binding="{Binding Year}" Value="2013">
                                            <Setter Property="Background" Value="Blue" />
                                            <Setter Property="Foreground" Value="Yellow" />
                                        </DataTrigger>
                                    </Style.Triggers>
                                </Style>
                            </TextBlock.Style>
                        </TextBlock>
                    </DataTemplate>
                </GridViewColumn.CellTemplate>
            </GridViewColumn>
        </GridView>
    </ListView.View>
</ListView>

结果:

在此处输入图像描述

第二种解决方案:

您可以为样式属性创建类。

public class CarProperty
{
    public SolidColorBrush Background { get; set; }
    public SolidColorBrush Foreground { get; set; }
    public FontWeight FontWeight { get; set; }
}

现在您可以为 Car 类中的每个属性添加适当的 CarProperty 属性:

public class Car
{
    public Car()
    {
        YearProperty = new CarProperty
        {
            Background = new SolidColorBrush(Colors.Transparent),
            Foreground = new SolidColorBrush(Colors.Black),
            FontWeight = FontWeights.Normal
        };
    }
    public string Name { get; set; }
    public string Make { get; set; }
    public string Year { get; set; }
    public CarProperty YearProperty { get; set; }
}

生成_点击:

private void generate_Click(object sender, RoutedEventArgs e)
{
    int i = 0;
    string[] name = { "Sentra", "IS", "Camry" };
    string[] make = { "Nissan", "Lexus", "Toyota" };
    string[] year = { "2003", "2011", "2013" };

    foreach (string s in name)
    {
        Car car = new Car();
        car.Name = name[i];
        car.Make = make[i];
        car.Year = year[i];

        if (year[i] == "2013")
        {
            car.YearProperty = new CarProperty();
            car.YearProperty.Background = new SolidColorBrush(Colors.Blue);
            car.YearProperty.Foreground = new SolidColorBrush(Colors.Yellow);
            car.YearProperty.FontWeight = FontWeights.Bold;
        }

        carList.Items.Add(car);

        i++;
    }
}

XAML:

<ListView x:Name="carList" VerticalAlignment="Top">                
    <ListView.View>
        <GridView>
            <GridViewColumn Header="Car" DisplayMemberBinding="{Binding Name}" Width="Auto"/>
            <GridViewColumn DisplayMemberBinding="{Binding Make}" Header="Make" Width="Auto"/>
            <GridViewColumn Header="Year" Width="Auto">
                <GridViewColumn.CellTemplate>
                    <DataTemplate>
                        <TextBlock Text="{Binding Year}">
                            <TextBlock.Style>
                                <Style TargetType="TextBlock">
                                    <Setter Property="Background" Value="{Binding YearProperty.Background}" />
                                    <Setter Property="Foreground" Value="{Binding YearProperty.Foreground}" />
                                    <Setter Property="FontWeight" Value="{Binding YearProperty.FontWeight}" />
                                </Style>
                            </TextBlock.Style>
                        </TextBlock>
                    </DataTemplate>
                </GridViewColumn.CellTemplate>
            </GridViewColumn>
        </GridView>
    </ListView.View>
</ListView>
于 2013-09-13T19:30:21.723 回答