C#列表框,每个项目的背景不同

C#列表框,每个项目的背景不同,c#,xaml,windows-phone-8,listbox,C#,Xaml,Windows Phone 8,Listbox,我有xaml中的列表框: <ListBox Name="feedListBox" Height="758" HorizontalAlignment="Center" Margin="0,10,0,0" VerticalAlignment="Top" Width="480" ScrollViewer.VerticalScrollBarVisibility="Auto" SelectionChanged="feedListBox_SelectionChanged" Grid.Row="1"

我有xaml中的列表框:

  <ListBox Name="feedListBox" Height="758" HorizontalAlignment="Center" Margin="0,10,0,0" VerticalAlignment="Top" Width="480" ScrollViewer.VerticalScrollBarVisibility="Auto" SelectionChanged="feedListBox_SelectionChanged" Grid.Row="1" Grid.Column="0" Grid.ColumnSpan="1" Background="White">
        <ListBox.ItemTemplate>
            <DataTemplate>
                <StackPanel VerticalAlignment="Top" Width="480">
                    <TextBlock FontWeight="Bold" FontSize="24" Name="feedTitle" TextWrapping="Wrap" Margin="12,0,0,0" HorizontalAlignment="Left" Foreground="#FF000000" Text="{Binding Title.Text, Converter={StaticResource RssTextTrimmer}}" />
                    <TextBlock Name="feedSummary" Foreground="#FF000000" TextWrapping="Wrap" Margin="12,0,0,0" Text="{Binding Summary.Text, Converter={StaticResource RssTextTrimmer}}" />
                    <TextBlock Name="feedPubDate" Foreground="#FF939393" Margin="12,0,10,10" Text="{Binding PublishDate.DateTime}" HorizontalAlignment="Right" />
                    <Border BorderThickness="1" Height="2" HorizontalAlignment="Center" VerticalAlignment="Top" Width="480" BorderBrush="Black" />
                </StackPanel>
            </DataTemplate>
        </ListBox.ItemTemplate>
  </ListBox>


如何为每个项目指定不同的背景?

向viewmodel添加一个
笔刷属性,并将
数据模板中的控件绑定到它

视图模型:

using System.ComponentModel;
using System.Windows.Media;
...
public class YourViewModel : INotifyPropertyChanged{
    ...
    private Brush _backgroundCol = Brushes.Red; //Default color
    public Brush BackgroundCol
    {
        get { return _backgroundCol; }
        set 
        {
            _backgroundCol = value;
            OnPropertyChanged("BackgroundCol");
        }
    }
    ...
}
xaml:


有关如何实现该接口的信息,请查看:

尝试以下操作:

XAML:


政务司司长:

公共类数据
{
公共字符串标题{get;set;}
公共字符串feedTitleBack{get;set;}
公共字符串摘要{get;set;}
公共字符串反馈汇总{get;set;}
公共字符串PublishDate{get;set;}
公共字符串feedPubDateBack{get;set;}
公共数据(){}
公共数据(字符串标题、字符串feedTitleBack、字符串摘要、字符串feedSummaryBack、字符串PublishDate、字符串feedPubDateBack)
{
这个.Title=Title;
this.feedTitleBack=feedTitleBack;
这个.总结=总结;
this.feedSummaryBack=feedSummaryBack;
this.PublishDate=PublishDate;
this.feedPubDateBack=feedPubDateBack;
}
}
void loadData()
{
List obj=新列表();
对象添加(新数据(“标题1”、“红色”、“摘要1”、“绿色”、“日期”、“蓝色”);
目标添加(新数据(“标题1”、“DD4B39”、“摘要1”、“006621”、“日期”、“DAB”);
feedListBox.ItemsSource=obj;
}
受保护的覆盖无效OnNavigatedTo(NavigationEventArgs e)
{
基地。导航到(e);
loadData();
}

为笔刷属性可能希望成为get、set的每个项目指定不同的颜色property@user1很好的一点-我已经更新了我的代码示例,以反映您的建议您解决了这个问题吗?如果是,请将对您帮助最大的答案标记为已接受答案。
<TextBlock Name="feedPubDate" Background="{Binding Path=BackgroundCol}" />
            <ListBox Name="feedListBox" Height="758" HorizontalAlignment="Center" Margin="0,10,0,0" VerticalAlignment="Top" Width="480" ScrollViewer.VerticalScrollBarVisibility="Auto" SelectionChanged="feedListBox_SelectionChanged" Grid.Row="1" Grid.Column="0" Grid.ColumnSpan="1" Background="White">
                <ListBox.ItemTemplate>
                    <DataTemplate>
                        <StackPanel VerticalAlignment="Top" Width="480">
                            <Grid Background="{Binding feedTitleBack}">
                                <TextBlock FontWeight="Bold" FontSize="24" Name="feedTitle" TextWrapping="Wrap" Margin="12,0,0,0" HorizontalAlignment="Left" Foreground="#FF000000" Text="{Binding Title}"/>
                            </Grid>
                            <Grid Background="{Binding feedSummaryBack}">
                                <TextBlock Name="feedSummary" Foreground="#FF000000" TextWrapping="Wrap" Margin="12,0,0,0" Text="{Binding Summary}" />
                            </Grid>
                            <Grid Background="{Binding feedPubDateBack}">
                                <TextBlock Name="feedPubDate" Foreground="#FF939393" Margin="12,0,10,10" Text="{Binding PublishDate}" HorizontalAlignment="Right" />
                            </Grid>
                            <Border BorderThickness="1" Height="2" HorizontalAlignment="Center" VerticalAlignment="Top" Width="480" BorderBrush="Black" />
                        </StackPanel>
                    </DataTemplate>
                </ListBox.ItemTemplate>
            </ListBox>
public class data
{
    public string Title { get; set; }
    public string feedTitleBack { get; set; }
    public string Summary { get; set; }
    public string feedSummaryBack { get; set; }
    public string PublishDate { get; set; }
    public string feedPubDateBack { get; set; }

    public data() { }
    public data(string Title, string feedTitleBack, string Summary, string feedSummaryBack, string PublishDate, string feedPubDateBack)
    {
        this.Title = Title;
        this.feedTitleBack = feedTitleBack;
        this.Summary = Summary;
        this.feedSummaryBack = feedSummaryBack;
        this.PublishDate = PublishDate;
        this.feedPubDateBack = feedPubDateBack;
    }
}

    void loadData()
    {
        List<data> obj = new List<data>();
        obj.Add(new data("Title1", "Red", "Summary1", "Green", "Date", "Blue"));
        obj.Add(new data("Title1", "#DD4B39", "Summary1", "#006621", "Date", "#1A0DAB"));
        feedListBox.ItemsSource = obj;
    }

protected override void OnNavigatedTo(NavigationEventArgs e)
{
    base.OnNavigatedTo(e);
    loadData();
}