程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> .NET網頁編程 >> 關於.NET >> 張高興的 UWP 開發筆記:橫向 ListView,uwplistview

張高興的 UWP 開發筆記:橫向 ListView,uwplistview

編輯:關於.NET

張高興的 UWP 開發筆記:橫向 ListView,uwplistview


  ListView 默認的排列方向是縱向 ( Orientation="Vertical" ) ,但如果我們需要橫向顯示的 ListView 怎麼辦?

  Blend for Visual Studio 現在就派上用場了。不只是 ListView ,其他的控件也可以用 Blend 定制你自己的 UI 樣式。

 

  下面新建一個項目 "HorizontalListViewDemo" ,用於演示橫向 ListView ,解決方案結構如下:( GitHub: https://github.com/ZhangGaoxing/uwp-demo/tree/master/HorizontalListViewDemo )

  由於只是一個演示項目,ListView 的綁定數據素材引自 Bob Tabor 的 UWP 入門開發視頻 https://mva.microsoft.com/zh-cn/training-courses/-windows-10--14541 。

 

  項目分析

  1. MainPage 的結構

  MainPage 由兩部分組成,一個標題,一個 ListView 。

<Grid Background="Black">
        <Grid.RowDefinitions>
            <RowDefinition Height="Auto" />
            <RowDefinition Height="*" />
        </Grid.RowDefinitions>
        <!--標題-->
        <StackPanel Margin="15,15,15,0">
            <TextBlock Text="Horizontal ListView Demo" 
                       FontSize="30" FontWeight="Bold"
                       Foreground="White" />
            <TextBlock Text="橫向 ListView" Foreground="White" />
        </StackPanel>

        <ListView Name="MyListView" Grid.Row="1"  />

    </Grid>

 

  2. 用 Blend 定制樣式

  首先右擊項目,點擊“在 Blend 中設計”。

  在“對象和時間線”中找到 "MyListView" ,右擊。

  在“編輯其他模板”中有 ItemTemplate,ItemContainerStyle,ItemsPanel 三個選項。ItemTemplate 用於數據綁定,數據綁定的模板一般是手寫完成,用 Blend 也是可以創建數據綁定模板的。ItemContainerStyle 是容器的樣式,說白了就是 ListView 中的 Item 的顯示樣式,像 Width,Background 等都可以在其中定制。ItemsPanel 是橫向 ListView 的關鍵,ListView 的顯示方向就在其中。下面是橫向 ListView 的 ItemsPanel xaml代碼。

        <!--橫向布局-->
        <ItemsPanelTemplate x:Key="HorizontalItemsPanelTemplate">
            <VirtualizingStackPanel Orientation="Horizontal"
                VerticalAlignment="Top"
                ScrollViewer.HorizontalScrollMode="Enabled"
                ScrollViewer.VerticalScrollMode="Disabled"/>
        </ItemsPanelTemplate>

 

  3. 所有代碼

  MainPage.xaml

<Page
    x:Class="HorizontalListViewDemo.MainPage"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="using:HorizontalListViewDemo"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    xmlns:data="using:HorizontalListViewDemo.Model"
    mc:Ignorable="d">

    <Page.Resources>
        <!--數據綁定模板-->
        <DataTemplate x:Key="DataTemplate" x:DataType="data:Book">
            <StackPanel Margin="8">
                <Image Source="{x:Bind CoverImage}" HorizontalAlignment="Center" Margin="0,0,0,5" Width="90" />
                <TextBlock Text="{x:Bind Title}" Foreground="White" HorizontalAlignment="Center" FontSize="16" />
                <TextBlock Text="{x:Bind Author}" Foreground="White" HorizontalAlignment="Center" FontSize="10" />
            </StackPanel>
        </DataTemplate>
        <!--容器模板-->
        <Style x:Key="HorizontalItemContainerStyle" TargetType="ListViewItem">
            <Setter Property="MinWidth" Value="{StaticResource SplitViewCompactPaneThemeLength}"/>
            <Setter Property="Padding" Value="0"/>
            <Setter Property="UseSystemFocusVisuals" Value="True" />
            <Setter Property="Template">
                <Setter.Value>
                    <ControlTemplate TargetType="ListViewItem">
                        <ListViewItemPresenter ContentTransitions="{TemplateBinding ContentTransitions}"
                        Control.IsTemplateFocusTarget="True"
                        SelectionCheckMarkVisualEnabled="False"
                        PointerOverBackground="{ThemeResource SystemControlHighlightListLowBrush}"
                        PointerOverForeground="{ThemeResource ListViewItemForegroundPointerOver}"
                        SelectedBackground="Transparent"
                        SelectedForeground="{ThemeResource SystemControlForegroundAccentBrush}"
                        SelectedPointerOverBackground="{ThemeResource SystemControlHighlightListLowBrush}"
                        PressedBackground="{ThemeResource SystemControlHighlightListMediumBrush}"
                        SelectedPressedBackground="{ThemeResource SystemControlHighlightListMediumBrush}"
                        DisabledOpacity="{ThemeResource ListViewItemDisabledThemeOpacity}"
                        HorizontalContentAlignment="Stretch"
                        VerticalContentAlignment="{TemplateBinding VerticalContentAlignment}"
                        ContentMargin="{TemplateBinding Padding}"/>
                    </ControlTemplate>
                </Setter.Value>
            </Setter>
        </Style>
        <!--橫向布局-->
        <ItemsPanelTemplate x:Key="HorizontalItemsPanelTemplate">
            <VirtualizingStackPanel Orientation="Horizontal"
                VerticalAlignment="Top"
                ScrollViewer.HorizontalScrollMode="Enabled"
                ScrollViewer.VerticalScrollMode="Disabled"/>
        </ItemsPanelTemplate>

    </Page.Resources>
    
    
    <Grid Background="Black">
        <Grid.RowDefinitions>
            <RowDefinition Height="Auto" />
            <RowDefinition Height="*" />
        </Grid.RowDefinitions>
        <!--標題-->
        <StackPanel Margin="15,15,15,0">
            <TextBlock Text="Horizontal ListView Demo" 
                       FontSize="30" FontWeight="Bold"
                       Foreground="White" />
            <TextBlock Text="橫向 ListView" Foreground="White" />
        </StackPanel>

        <ListView Name="MyListView" Grid.Row="1" 
                  SelectionMode="None" IsItemClickEnabled="True"
                  HorizontalAlignment="Center"
                  Margin="20" BorderThickness="1" BorderBrush="White"
                  ScrollViewer.HorizontalScrollMode="Enabled" ScrollViewer.HorizontalScrollBarVisibility="Auto"
                  ScrollViewer.VerticalScrollMode="Disabled"
                  ItemsSource="{x:Bind Books}"
                  ItemTemplate="{StaticResource DataTemplate}"
                  ItemContainer
                  ItemsPanel="{StaticResource HorizontalItemsPanelTemplate}" />

    </Grid>
</Page>

  MainPage.xaml.cs

using HorizontalListViewDemo.Model;
using System.Collections.Generic;
using Windows.UI.Xaml.Controls;

namespace HorizontalListViewDemo
{
    public sealed partial class MainPage : Page
    {
        private List<Book> Books;

        public MainPage()
        {
            this.InitializeComponent();
            Books = BookManager.GetBooks();
        }
    }
}

  Book.cs

using System.Collections.Generic;

namespace HorizontalListViewDemo.Model
{
    public class Book
    {
        public int BookId { get; set; }
        public string Title { get; set; }
        public string Author { get; set; }
        public string CoverImage { get; set; }
    }

    public class BookManager
    {
        public static List<Book> GetBooks()
        {
            var books = new List<Book>
            {
                new Book { BookId = 1, Title = "Vulpate", Author = "Futurum", CoverImage = "Assets/1.png" },
                new Book { BookId = 2, Title = "Mazim", Author = "Sequiter Que", CoverImage = "Assets/2.png" },
                new Book { BookId = 3, Title = "Elit", Author = "Tempor", CoverImage = "Assets/3.png" },
                new Book { BookId = 4, Title = "Etiam", Author = "Option", CoverImage = "Assets/4.png" },
                new Book { BookId = 5, Title = "Feugait Eros Libex", Author = "Accumsan", CoverImage = "Assets/5.png" },
                new Book { BookId = 6, Title = "Nonummy Erat", Author = "Legunt Xaepius", CoverImage = "Assets/6.png" },
                new Book { BookId = 7, Title = "Nostrud", Author = "Eleifend", CoverImage = "Assets/7.png" },
                new Book { BookId = 8, Title = "Per Modo", Author = "Vero Tation", CoverImage = "Assets/8.png" },
                new Book { BookId = 9, Title = "Suscipit Ad", Author = "Jack Tibbles", CoverImage = "Assets/9.png" },
                new Book { BookId = 10, Title = "Decima", Author = "Tuffy Tibbles", CoverImage = "Assets/10.png" },
                new Book { BookId = 11, Title = "Erat", Author = "Volupat", CoverImage = "Assets/11.png" },
                new Book { BookId = 12, Title = "Consequat", Author = "Est Possim", CoverImage = "Assets/12.png" },
                new Book { BookId = 13, Title = "Aliquip", Author = "Magna", CoverImage = "Assets/13.png" }
            };

            return books;
        }
    }
}

  效果圖

 

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved