ListView中的逻辑用于绑定项

时间:2013-09-13 19:20:24

标签: c# wpf listview data-binding

我有三个数据数组,用于填充ListView <{1}}

我可以在Binding项目中添加逻辑来更改单元格的DisplayMemberBinding前景或背景属性吗?

的Xaml:

ListView

CS:

<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>

1 个答案:

答案 0 :(得分: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>

结果:

enter image description here

第二个解决方案:

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

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; }
}

generate_Click:

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>