listbox – 如何将不同的背景颜色添加到备用行以列出框项目windo
发布时间:2020-12-13 22:27:08 所属栏目:Windows 来源:网络整理
导读:我是 Windows手机开发的新手. 我在列表框中显示数据.对于列表框中的所有行,背景颜色相同. 但我想为备用行添加两种不同的颜色来列出框项目. 下面是列表视图的代码. Grid x:Name="ContentPanel" Grid.Row="1" Margin="0,10,0" Background="White" ListBox Marg
我是
Windows手机开发的新手.
我在列表框中显示数据.对于列表框中的所有行,背景颜色相同. 下面是列表视图的代码. <Grid x:Name="ContentPanel" Grid.Row="1" Margin="0,10,0" Background="White"> <ListBox Margin="6,6,-6,6" Name="itemslb" SelectionChanged="itemList_SelectionChanged"> <ListBox.ItemTemplate> <DataTemplate> <Grid x:Name="listItem"> <Grid.RowDefinitions> <RowDefinition Height="100"/> <RowDefinition Height="10"/> </Grid.RowDefinitions> <StackPanel Grid.Row="0" Orientation="Horizontal" Height="100" Margin="0,0"> <StackPanel Width="85" Height="100" > <StackPanel.Background> <ImageBrush x:Name="defImage" ImageSource="/DailyFeeds;component/Images/default.png" Stretch="None"></ImageBrush> </StackPanel.Background> <Image Source="{Binding ImageUrl}" VerticalAlignment="Top" Height="90" Width="85" Margin="0,0" Stretch="Fill" /> </StackPanel> <StackPanel Width="370"> <TextBlock Text="{Binding Title}" Foreground="SlateBlue" FontSize="25" TextWrapping="Wrap" /> <TextBlock Text="{Binding Date}" Foreground="#FFC8AB14" FontSize="20"/> </StackPanel> </StackPanel> <Image x:Name="line" Grid.Row="1" Width="460" HorizontalAlignment="Center" Margin="0,5,0" Source="/DailyFeeds;component/Images/separator.png" /> </Grid> </DataTemplate> </ListBox.ItemTemplate> </ListBox> </Grid> 我们怎么做到这一点. 谢谢 解决方法
创建一个转换器类
public class AlternateRowColour : IValueConverter { bool isAlternate; SolidColorBrush even = new SolidColorBrush(Colors.Transparent); // Set these two brushes to your alternating background colours. SolidColorBrush odd = new SolidColorBrush(Color.FromArgb(255,241,241)); public object Convert(object value,Type targetType,object parameter,System.Globalization.CultureInfo culture) { isAlternate = !isAlternate; return isAlternate ? even : odd ; } public object ConvertBack(object value,System.Globalization.CultureInfo culture) { throw new NotImplementedException(); } } 将转换器添加到页面 <UserControl xmlns:conv="clr-namespace:MyApplication.Converters" <UserControl.Resources> <conv:AlternateRowColour x:Key="RowColour" /> </UserControl.Resources> </UserControl> 你的listBox <ListBox.ItemTemplate> <DataTemplate> <StackPanel Background="{Binding Converter={StaticResource RowColour}}"> // your stackPanel <!-- layout XAML --> </StackPanel> </DataTemplate> </ListBox.ItemTemplate> (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
相关内容
- Windows窗体中的打印面板C#
- remote – 如何在Windows上使用IPMI?
- windows – 设置EXE VersionInfo
- 强制emacs最近使用recentf忽略指定文件的文件(例如.windows
- 在windows下运行linux gcc编译的程序
- windows – 检测具有节点的系统路径上是否存在可执行文件
- Windows Phone – 在Windows Phone 8中设置广告
- windows-server-2008 – Windows网络SNMP流量溢出?
- .net – System.Drawing(GDI)vs System.Windows.Media(WPF)
- Windows – 主机关联/负载平衡跨VPN的Microsoft RDS