获取ListView项目属性

时间:2017-02-28 21:59:24

标签: c# wpf xaml listview uwp

我手动将项目添加到ListView。我是UWP和xaml的新手。 这是我的xaml和c#代码:

public sealed partial class CalendarFlyout : SettingsFlyout
{
    public CalendarFlyout()
    {
        this.InitializeComponent();
        Width = 450;
        for (int i = 0; i < GlobalVars.table.Count; i++)
        {
                calendarFlyout.Items.Add(new Items { Time = sSplit[0], Country = sSplit[1], Title = sSplit[2], Results = sSplit[3] + "|" + sSplit[4] + "|" + sSplit[5], FlagImage = imagePath, bull1 = images[0], bull2 = images[1], bull3 = images[2], Color = new SolidColorBrush(Colors.LimeGreen)});
        }
        //change background here
    }
}

public class Items
{
    public string Time { get; set; }
    public string Country { get; set; }
    public string Title { get; set; }
    public string Results {get; set; }
    public string FlagImage { get; set; }
    public string bull1 { get; set; }
    public string bull2 { get; set; }
    public string bull3 { get; set; }
    public Brush Color { get; set; }
}

XAML:

<ListView x:Name="calendarFlyout" BorderThickness="0" ItemsSource="{Binding}" Width="450"> 
    <ListView.ItemTemplate>
        <DataTemplate>
            <Border Name="bord1" BorderBrush="#FFCDCDCD" BorderThickness="0,0,0,1" Width="450" VerticalAlignment="Stretch" HorizontalAlignment="Left">
                <Grid HorizontalAlignment="Left" Width="450" Height="50" Background="{Binding Color}">
                    <TextBlock x:Name="timeText" Text="{Binding Time}" Margin="0,0"/>
                    <TextBlock Name="countryText" Text="{Binding Country}" Margin="65,0,0,0"/>
                    <TextBlock Name="newsText" Text="{Binding Title}" Margin="120,0,0,0"/>
                    <TextBlock Name="resultText" Text="{Binding Results}" Margin="120,30,0,0" FontWeight="Bold"/>
                    <Image Margin="0,15,440,0" Source="{Binding bull1}" Stretch="Uniform"/>
                    <Image Margin="20,15,420,0" Source="{Binding bull2}" Stretch="Uniform"/>
                    <Image Margin="40,15,400,0" Source="{Binding bull3}" Stretch="Uniform"/>
                    <Image Name="flag" Margin="65,20,355,10" Source="{Binding FlagImage}" Stretch="Uniform"/>

                </Grid>
            </Border>
        </DataTemplate>
    </ListView.ItemTemplate>
</ListView>

我正在尝试更改特定项目的背景颜色。我该如何访问该项目?有没有办法在创建项目之后设置背景颜色,而不是在创建项目时?

2 个答案:

答案 0 :(得分:2)

首先,您可以这样访问ListView的项目:

Items targetItem= calendarFlyout.Items[2] as Items;

然后,如果要通过将目标Brush值分配给该项来更改其背景颜色,则需要使模型类实现接口example,因此模型类的定义将如下所示:

public class Items : INotifyPropertyChanged
{
    public string Time { get; set; }
    public string Country { get; set; }
    public string Title { get; set; }
    public string Results { get; set; }
    public string FlagImage { get; set; }
    public string bull1 { get; set; }
    public string bull2 { get; set; }
    public string bull3 { get; set; }

    private Brush _brush;
    public Brush Color
    {
        get { return _brush; }
        set
        {
            _brush = value;
            RaisePropertyChanged("Color");
        }
    }

    private void RaisePropertyChanged(string propertyName)
    {
        if (PropertyChanged != null)
        {
            PropertyChanged.Invoke(this, new PropertyChangedEventArgs(propertyName));
        }
    }

    public event PropertyChangedEventHandler PropertyChanged;
}

最后,您可以更改其背景颜色:

Items targetItem = calendarFlyout.Items[2] as Items;
targetItem.Color = new SolidColorBrush(Colors.Red);

此外,作为一个建议,我认为您最好更深入地了解INotifyPropertyChanged和MVVM设计模式。

答案 1 :(得分:0)

calendarFlyout.Items [2] .BackColor = Color.Blue;