【问题标题】:Get ListView item properties获取 ListView 项属性
【发布时间】: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# wpf xaml listview uwp


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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-08-31
      • 1970-01-01
      • 2012-06-26
      • 2020-06-19
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多