首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式碼之家 >> 編程語言 >  >> .NET 網頁程式設計 >> 關於.NET

Windows 8 Store Apps 開發實戰(5):集合控件基礎 — ComboBox、ListBox、FlipView、ItemsControl 與 ItemsPresenter

     編輯:關於.NET

前言

集合控件是資料驅動 UI 的核心。WinRT 提供 ComboBox(下拉選單)、ListBox(多選列表)、FlipView(翻頁視圖)、ItemsControl(基礎集合容器)四大控件,均繼承自 ItemsControl。本文將並列示範四者的資料綁定、項目模板、容器樣式、面板自定義與 ItemsPresenter 角色。

一、ComboBox — 下拉選單

三種資料提供方式

  1. XAML 靜態項目:<ComboBoxItem Content="..."/>
  2. 後台綁定 + DisplayMemberPath:指定顯示欄位
  3. ItemTemplate 完整自定義:多欄位、豐富排版

完整範例

<StackPanel Margin="120 0 0 0">
    <!-- 1. XAML 靜態 -->
    <ComboBox x:Name="comboBox" Width="200" Margin="5" HorizontalAlignment="Left">
        <ComboBoxItem Content="ComboBoxItem1"/>
        <ComboBoxItem Content="ComboBoxItem2"/>
        <ComboBoxItem Content="ComboBoxItem3"/>
    </ComboBox>

    <!-- 2. 綁定 + DisplayMemberPath -->
    <ComboBox x:Name="comboBoxWithBinding"
              DisplayMemberPath="Name"
              MaxDropDownHeight="100"
              Width="200" Margin="5" HorizontalAlignment="Left"/>

    <!-- 3. ItemTemplate 自定義 -->
    <ComboBox ItemsSource="{Binding ItemsSource, ElementName=comboBoxWithBinding}"
              MaxDropDownHeight="100" Width="200" Margin="5" HorizontalAlignment="Left">
        <ComboBox.ItemTemplate>
            <DataTemplate>
                <StackPanel Orientation="Horizontal">
                    <TextBlock Text="{Binding Name}"/>
                    <TextBlock Text="{Binding Age}" Margin="5 0 0 0"/>
                </StackPanel>
            </DataTemplate>
        </ComboBox.ItemTemplate>
    </ComboBox>
</StackPanel>

後台綁定

protected override void OnNavigatedTo(NavigationEventArgs e)
{
    comboBoxWithBinding.ItemsSource = TestData.GetEmployees();
}

關鍵屬性與事件

  • IsDropDownOpen:下拉是否展開
  • MaxDropDownHeight:下拉最大高度
  • DropDownOpened/DropDownClosed:展開/收合事件

二、ListBox — 多選列表框

三種資料提供方式(同 ComboBox)

<StackPanel Margin="120 0 0 0" Orientation="Horizontal">
    <!-- 1. XAML 靜態 -->
    <ListBox x:Name="listBox" Width="200" Height="300" Margin="5"
             HorizontalAlignment="Left" VerticalAlignment="Top">
        <ListBoxItem Content="ListBoxItem1"/>
        <ListBoxItem Content="ListBoxItem2"/>
        <ListBoxItem Content="ListBoxItem3"/>
    </ListBox>

    <!-- 2. 綁定 + DisplayMemberPath -->
    <ListBox x:Name="listBoxWithBinding"
             SelectionMode="Multiple"
             DisplayMemberPath="Name"
             Width="200" Height="300" Margin="5"
             HorizontalAlignment="Left" VerticalAlignment="Top"/>

    <!-- 3. ItemTemplate + ItemsPanel (VirtualizingStackPanel) -->
    <ListBox ItemsSource="{Binding ItemsSource, ElementName=listBoxWithBinding}"
             SelectionMode="Multiple" Width="200" Height="300" Margin="5"
             HorizontalAlignment="Left" VerticalAlignment="Top">
        <ListBox.ItemTemplate>
            <DataTemplate>
                <StackPanel Orientation="Horizontal">
                    <TextBlock Text="{Binding Name}"/>
                    <TextBlock Text="{Binding Age}" Margin="5 0 0 0"/>
                </StackPanel>
            </DataTemplate>
        </ListBox.ItemTemplate>
        <ListBox.ItemsPanel>
            <ItemsPanelTemplate>
                <VirtualizingStackPanel Orientation="Vertical"
                                        VirtualizationMode="Recycling"/>
            </ItemsPanelTemplate>
        </ListBox.ItemsPanel>
    </ListBox>
</StackPanel>

後台綁定

protected override void OnNavigatedTo(NavigationEventArgs e)
{
    listBoxWithBinding.ItemsSource = TestData.GetEmployees();
}

關鍵屬性與方法

  • SelectionMode:Single/Multiple(滑鼠多選)/Extended(Ctrl/Shift 多選)
  • ScrollIntoView(item):捲動到指定項目
  • SelectAll():全選

三、FlipView — 翻頁/輪播視圖

適合圖片瀏覽、教學逐步、單項目聚焦閱讀。支援水平(預設)與垂直翻頁。

XAML 靜態項目

<FlipView Width="320" Height="240" HorizontalAlignment="Left" Background="Black">
    <FlipView.Items>
        <FlipViewItem><TextBlock FontSize="26.667" Text="I am webabcd"/></FlipViewItem>
        <FlipViewItem><Image Source="/Assets/Logo.png" Stretch="Fill"/></FlipViewItem>
    </FlipView.Items>
</FlipView>

後台綁定 + 垂直翻頁 + 容器置中

<FlipView x:Name="flipView" Width="320" Height="240"
          Background="Yellow" HorizontalAlignment="Left" Margin="0 10 0 0">
    <FlipView.ItemContainerStyle>
        <Style TargetType="FlipViewItem">
            <Setter Property="HorizontalContentAlignment" Value="Center"/>
            <Setter Property="VerticalContentAlignment" Value="Center"/>
        </Style>
    </FlipView.ItemContainerStyle>

    <FlipView.ItemsPanel>
        <ItemsPanelTemplate>
            <StackPanel Orientation="Vertical"/>  <-- 垂直翻頁 -->
        </ItemsPanelTemplate>
    </FlipView.ItemsPanel>

    <FlipView.ItemTemplate>
        <DataTemplate>
            <Grid Background="Black">
                <TextBlock Text="{Binding Name}" FontSize="26.667"/>
                <TextBlock Text="{Binding Age}" FontSize="26.667" Margin="0 40 0 0"/>
            </Grid>
        </DataTemplate>
    </FlipView.ItemTemplate>
</FlipView>

後台綁定

protected override void OnNavigatedTo(NavigationEventArgs e)
{
    flipView.ItemsSource = TestData.GetEmployees();
}

四、ItemsControl 與 ItemsPresenter — 集合控件的基石

ItemsControl 是最基礎的集合容器(無選取、無虛擬化、無預設面板)。ListBox、ComboBox、FlipView、GridView、ListView 皆間接繼承它。

ItemsPresenter 的角色

在 ControlTemplate 中,ItemsPresenter 是「項目面板」的佔位符,負責呈現 ItemsPanel 產生的所有項目容器。它呈現的是 Items 集合,而非單一 Item。

範例:自定義 ItemsControl 模板 + ItemsPresenter

<ItemsControl HorizontalAlignment="Left">
    <ItemsControl.Items>
        <Rectangle Width="100" Height="100" Fill="Red"/>
        <Rectangle Width="100" Height="100" Fill="Green"/>
        <Rectangle Width="100" Height="100" Fill="Blue"/>
    </ItemsControl.Items>

    <ItemsControl.Template>
        <ControlTemplate>
            <Border BorderBrush="Orange" BorderThickness="1" Width="400" Height="400">
                <!-- ItemsPresenter 置中呈現所有項目 -->
                <ItemsPresenter Margin="10"
                                VerticalAlignment="Center"
                                HorizontalAlignment="Center"/>
            </Border>
        </ControlTemplate>
    </ItemsControl.Template>
</ItemsControl>

對照表:Presenter 家族

Presenter所屬控件呈現內容
ContentPresenterContentControl單一 Content
ItemsPresenterItemsControlItems 集合(透過 ItemsPanel)
ScrollContentPresenterScrollViewerContent(支援捲動)

五、小結

  1. ComboBox:下拉選單,DisplayMemberPath 簡單顯示,ItemTemplate 豐富排版,MaxDropDownHeight 控制高度
  2. ListBox:多選列表,SelectionMode 三模式,VirtualizingStackPanel 大量資料虛擬化,可水平排列
  3. FlipView:翻頁視圖,ItemsPanel 切換水平/垂直,ItemContainerStyle 置中內容
  4. ItemsControl:基礎集合容器,ItemsPresenter 在模板中呈現項目面板,理解它是客製化集合控件的鑰匙

這四大控件涵蓋了從「單選下拉」到「多選列表」、「翻頁瀏覽」再到「完全自定義集合」的完整光譜,是構建資料驅動 UI 的基石工具箱。


原始碼下載:https://files.cnblogs.com/webabcd/Windows8.rar

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式碼之家 All Rights Reserved