积极答复者
ListView 怎么控制每一个Item的Background或Foreground

问题
-
一个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.- 已建议为答案 Xavier Xie-MSFT 2016年6月8日 2:23
- 已标记为答案 Airforce_jie 2016年6月8日 6:13
2016年6月8日 1:31
全部回复
-
您好 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.- 已建议为答案 Xavier Xie-MSFT 2016年6月8日 2:23
- 已标记为答案 Airforce_jie 2016年6月8日 6:13
2016年6月8日 1:31