Windows phone 7 列表框项目模板中的备用行样式:Windows Phone
在我们的Windows Phone项目中,我们有一个ListBox ItemTemplate。我们正在绑定一些数据。在显示数据时,我们希望突出显示每个备用行。就像下面。。。有人能帮忙吗,我怎样才能做到这一点 第1行 第2行 第3行 第4行 我绑定此数据源的项目列表如下所示Windows phone 7 列表框项目模板中的备用行样式:Windows Phone,windows-phone-7,windows-phone-8,listbox,windows-phone-8.1,Windows Phone 7,Windows Phone 8,Listbox,Windows Phone 8.1,在我们的Windows Phone项目中,我们有一个ListBox ItemTemplate。我们正在绑定一些数据。在显示数据时,我们希望突出显示每个备用行。就像下面。。。有人能帮忙吗,我怎样才能做到这一点 第1行 第2行 第3行 第4行 我绑定此数据源的项目列表如下所示 public static readonly IList<String> Metrics = new ReadOnlyCollection<string>
public static readonly IList<String> Metrics =
new ReadOnlyCollection<string>
(new List<String> {
"ATM not working",
"Store out of business",
"ATM not there",
"Wrong store Address",
"Wrong store Name",
"Wrong ATM features Listed"
});
一种可能不是最好的解决方案是在Item类中为BackgroundBrush创建一个属性,然后可以将任何笔刷指定给背景:
<ListBox.ItemTemplate>
<DataTemplate>
<Grid Background={Binding BackBrush}>
<StackPanel>
<TextBlock Text="{Binding Text}" Foreground="Black" FontSize="32" FontWeight="SemiBold" FontFamily="Calibri" HorizontalAlignment="Stretch"></TextBlock>
<HyperlinkButton xml:space="preserve" Foreground="Black" FontSize="4"></HyperlinkButton>
</StackPanel>
</Grid>
</DataTemplate>
</ListBox.ItemTemplate>
你可以举一个例子:
yourItemSource.Add(new Item { Text = "First", BackBrush = (App.Current as App).Resources["PhoneAccentBrush"] as SolidColorBrush});
由于您有一个只读集合,您可以将您的项目资源设置为:
public class Item
{
public string Text { get; set; }
public SolidColorBrush BackBrush { get; set; }
}
myList.ItemsSource = Metrics.Select((a, b) => new Item { Text = a, BackBrush = b % 2 == 0 ? (App.Current as App).Resources["PhoneAccentBrush"] as SolidColorBrush : new SolidColorBrush(Windows.UI.Colors.Transparent) }).ToList();
这可能需要一些改进,但应该可以工作。谢谢我在问题中添加了我的Itemsource,我正在绑定到网格。您能帮助我如何准备作为itemsource的Metrics参数吗。@Subhamoy我已经添加了一个可能的解决方案,您可以如何将度量值与BackgroundBrush一起分配。别忘了使用System.Linq添加
myList.ItemsSource = Metrics.Select((a, b) => new Item { Text = a, BackBrush = b % 2 == 0 ? (App.Current as App).Resources["PhoneAccentBrush"] as SolidColorBrush : new SolidColorBrush(Windows.UI.Colors.Transparent) }).ToList();