加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > Windows > 正文

Windows Phone 8.1中ListView中行的交替颜色

发布时间:2020-12-14 04:36:07 所属栏目:Windows 来源:网络整理
导读:我已经创建了一个 Windows Phone 8.1运行时应用程序. 我正在使用ListView控件. 我想交替每个背景行的颜色. 搜索后我发现这个链接a previous answer. 但是这会给出标记错误.一方面没有’AlternationCount’属性.我假设这是因为它不是SilverLight而是RT? 如果
我已经创建了一个 Windows Phone 8.1运行时应用程序.

我正在使用ListView控件.

我想交替每个背景行的颜色.

搜索后我发现这个链接a previous answer.

但是这会给出标记错误.一方面没有’AlternationCount’属性.我假设这是因为它不是SilverLight而是RT?

如果有人可以给我发一个链接,因为我很难找到一个简单的例子.更好的一个简单的代码示例将不胜感激.

我的建议是使用带有附加DependencyProperties的Converter类.当您初始化转换器时,您可以定义要引用的项目集合以及背景的备用画笔列表.它可以像这样寻找例子:
public class AlternateConverter : DependencyObject,IValueConverter
{
    public List<SolidColorBrush> AlternateBrushes
    {
        get { return (List<SolidColorBrush>)GetValue(AlternateBrushesProperty); }
        set { SetValue(AlternateBrushesProperty,value); }
    }

    public static readonly DependencyProperty AlternateBrushesProperty =
        DependencyProperty.Register("AlternateBrushes",typeof(List<SolidColorBrush>),typeof(AlternateConverter),new PropertyMetadata(new List<SolidColorBrush>()));

    public object CurrentList
    {
        get { return GetValue(CurrentListProperty); }
        set { SetValue(CurrentListProperty,value); }
    }

    public static readonly DependencyProperty CurrentListProperty =
        DependencyProperty.Register("CurrentList",typeof(object),new PropertyMetadata(null));

    public object Convert(object value,Type targetType,object parameter,string language)
    { return AlternateBrushes[(CurrentList as IList).IndexOf(value) % AlternateBrushes.Count]; }

    public object ConvertBack(object value,string language)
    { throw new NotImplementedException(); }
}

一旦定义并创建备用画笔列表:

// somewhere in your DataContext
private List<SolidColorBrush> brushes = new List<SolidColorBrush> { new SolidColorBrush(Colors.Red),new SolidColorBrush(Colors.Blue) };
public List<SolidColorBrush> Brushes { get { return brushes; } }

你可以这样使用:

<ListView x:Name="myList" ItemsSource={Binding MyItems}>
  <ListView.Resources>
    <local:AlternateConverter CurrentList="{Binding ElementName=myList,Path=ItemsSource}" 
                                      AlternateBrushes="{Binding Brushes}"
                                      x:Key="AlternateConverter"/>
  </ListView.Resources>
  <ListView.ItemTemplate>
     <DataTemplate>
       <Border Background="{Binding Converter={StaticResource AlternateConverter}}">
          <!-- your itemtemplate -->
       </Border>
     </DataTemplate>
  </ListView.ItemTemplate>
</ListView>

这个解决方案应该是有效的,虽然它有价值类型的IList可能有问题.这里也不应该是延迟创建的问题,因为它直接从列表中搜索索引.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读