我正在手动将项目添加到我的 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>
我正在尝试更改特定项目的背景颜色。我怎样才能访问该项目?有没有办法在创建项目之后而不是创建项目时设置背景颜色?
最佳答案
首先,您可以像这样访问 ListView 的项目:
Items targetItem= calendarFlyout.Items[2] as Items;
然后,如果您想通过为该项目分配目标画笔值来更改其背景颜色,您需要使您的模型类实现接口(interface) 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 设计模式有更深入的了解。
关于c# - 获取ListView项目属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42519523/