【发布时间】: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>
我正在尝试更改特定项目的背景颜色。如何访问该项目?有没有办法在创建项目之后设置背景颜色,而不是在创建项目时设置?
【问题讨论】:
-
您能否访问要在代码中更改 Color 属性的 Items 对象,或者您真的需要访问 Grid 吗?
-
推荐使用
Binding。