获取 ListView 项属性

Posted

技术标签:

【中文标题】获取 ListView 项属性【英文标题】:Get ListView item properties 【发布时间】:2017-07-20 01:26:15 【问题描述】:

我正在手动将项目添加到我的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>

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

【问题讨论】:

见C# Change A Button's Background Color 和Set background color of WPF Textbox in C# code。他们有帮助吗? 您能否访问要在代码中更改 Color 属性的 Items 对象,或者您真的需要访问 Grid 吗? 推荐使用Binding 【参考方案1】:

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

【讨论】:

【参考方案2】:

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

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

那么,如果你想通过将目标Brush值分配给该项目来改变它的背景颜色,你需要让你的模型类实现接口INotifyPropertyChanged,所以你的模型类的定义是这样的:

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 设计模式有更深入的了解。

【讨论】:

以上是关于获取 ListView 项属性的主要内容,如果未能解决你的问题,请参考以下文章

vb listview 选中项的列值怎么获取

listview 如何总让最后一项选中?????

ASP.NET中如何获取listview指定列的值

绑定到 ListView 的 SelectedItem 属性时设置初始选定项

获取 ListView 可见项

JQuery Mobile - ListView - 获取选中项