locked
ListView 怎么控制每一个Item的Background或Foreground RRS feed

  • 问题

  • 一个ListVeiw,其ItemTemplater是一个简单的Textblock

         <DataTemplate x:Key="listItemTem">
                <TextBlock  Text="{Binding ContentName}" Foreground="#999999" FontSize="20" />
            </DataTemplate>

    绑定到一个简单的对象上

    class A

    {

      public string ContentName{set;get;}

     public string status {set;get;} 

    }

    数据都能正常展示,我在想根据A类中的status动态改变TextBlock的Foreground,该怎么处理?(wp8.1开发)

    2016年6月7日 8:29

答案

  • 您好 Airforce_jie,

    您可以写个Converter类,将Status属性绑定到Foreground属性时,指定这个转换类,这样就能够将string类型的status属性值转换成对应的Brush类型的值,我做了个例子供你参考:

    <Page.Resources>
            <local:StatusConverter x:Key="StatusConvert"></local:StatusConverter>
        </Page.Resources>
    
        <Grid Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">
            <ListView x:Name="listview">
                <ListView.ItemTemplate>
                    <DataTemplate>
                        <TextBlock  Text="{Binding ContentName}" Foreground="{Binding status,Converter={StaticResource StatusConvert}}" FontSize="20" />
                    </DataTemplate>
                </ListView.ItemTemplate>
            </ListView>
        </Grid>
    class StatusConverter : IValueConverter
        {
            public object Convert(object value, Type targetType, object parameter, string language)
            {
                if (value != null)
                {
                    string v = value.ToString();
                    switch (v)
                    {
                        case "s1": return new SolidColorBrush(Colors.Red);
                        case "s2": return new SolidColorBrush(Colors.Blue);
                    }
                }
                return new SolidColorBrush(Colors.Transparent);
            }
    
            public object ConvertBack(object value, Type targetType, object parameter, string language)
            {
                throw new NotImplementedException();
            }
        }
    public sealed partial class MainPage : Page
        {
            public MainPage()
            {
                this.InitializeComponent();
                ObservableCollection<A> coll = new ObservableCollection<A>();
                coll.Add(new A() {ContentName="abc",status="s1" });
                coll.Add(new A() { ContentName = "abc", status = "s2" });
                listview.ItemsSource = coll;
            }
        }
    
        public class A
        {
            public string ContentName { set; get; }
            public string status { set; get; }
        }




    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    2016年6月8日 1:31

全部回复

  • 可以使用DataTemplateSelector

    参考这篇blog:

    http://www.cnblogs.com/manupstairs/p/5240282.html

    2016年6月7日 13:43
  • 您好 Airforce_jie,

    您可以写个Converter类,将Status属性绑定到Foreground属性时,指定这个转换类,这样就能够将string类型的status属性值转换成对应的Brush类型的值,我做了个例子供你参考:

    <Page.Resources>
            <local:StatusConverter x:Key="StatusConvert"></local:StatusConverter>
        </Page.Resources>
    
        <Grid Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">
            <ListView x:Name="listview">
                <ListView.ItemTemplate>
                    <DataTemplate>
                        <TextBlock  Text="{Binding ContentName}" Foreground="{Binding status,Converter={StaticResource StatusConvert}}" FontSize="20" />
                    </DataTemplate>
                </ListView.ItemTemplate>
            </ListView>
        </Grid>
    class StatusConverter : IValueConverter
        {
            public object Convert(object value, Type targetType, object parameter, string language)
            {
                if (value != null)
                {
                    string v = value.ToString();
                    switch (v)
                    {
                        case "s1": return new SolidColorBrush(Colors.Red);
                        case "s2": return new SolidColorBrush(Colors.Blue);
                    }
                }
                return new SolidColorBrush(Colors.Transparent);
            }
    
            public object ConvertBack(object value, Type targetType, object parameter, string language)
            {
                throw new NotImplementedException();
            }
        }
    public sealed partial class MainPage : Page
        {
            public MainPage()
            {
                this.InitializeComponent();
                ObservableCollection<A> coll = new ObservableCollection<A>();
                coll.Add(new A() {ContentName="abc",status="s1" });
                coll.Add(new A() { ContentName = "abc", status = "s2" });
                listview.ItemsSource = coll;
            }
        }
    
        public class A
        {
            public string ContentName { set; get; }
            public string status { set; get; }
        }




    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    2016年6月8日 1:31
  • 学习到新东西了,十分详细,非常感谢   !!!!!
    2016年6月8日 2:14