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

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>

(编辑:李大同)

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

    推荐文章
      热点阅读