Avalonia UI
首页支持GitHub 仓库English Doc
  • 👋欢迎
  • 文档
    • ⚡快速开始
      • IDE 支持
        • JetBrains Rider 设置
      • 使用 Avalonia 开发
        • Model-View-ViewModel 模式(MVVM)
        • 控件和布局
        • 数据绑定
        • 图像和动画
      • Windows
      • UserControls
      • 资产
      • 开发者工具
      • 错误和警告日志
      • 未处理的异常
      • 应用生命周期
    • 🔁数据绑定
      • 数据上下文
      • 变化通知
      • 绑定
      • 编译绑定
      • 与控件绑定
      • 转换绑定值
      • 绑定到命令
      • 绑定到任务和可观察对象
      • 使用代码进行绑定
      • 在控件模板中实现绑定
      • 绑定Classes
      • 创建和绑定到附加属性
      • Data Validation
    • 🎨样式
      • 样式
      • 选择器
      • 资源
      • 疑难解答
    • 🧰控件
      • AutoCompleteBox
      • Border
      • Buttons
        • Button
        • RepeatButton
        • RadioButton
        • ToggleButton
        • ButtonSpinner
        • SplitButton
        • ToggleSplitButton
      • Calendar
      • Canvas
      • Carousel
      • CheckBox
      • ComboBox
      • ContentControl
      • ContextMenu
      • Decorator
      • DataGrid
        • DataGridColumns
      • DatePicker
      • DockPanel
      • Expander
      • Flyouts
      • Grid
      • GridSplitter
      • Image
      • ItemsControl
      • ItemsRepeater
      • LayoutTransformControl
      • ListBox
      • MaskedTextBox
      • Menu
      • NativeMenu
      • NumericUpDown
      • Panel
      • ProgressBar
      • RelativePanel
      • ScrollBar
      • ScrollViewer
      • Separator
      • Slider
      • SplitView
      • StackPanel
      • TabControl
      • TabStrip
      • TextBlock
      • TrayIcon
      • TreeDataGrid
        • Creating a Hierarchical TreeDataGrid
        • Creating a Flat TreeDataGrid
        • TreeDataGrid column types
      • TimePicker
      • TextBox
      • ToolTip
      • TreeView
      • TransitioningContentControl
      • UserControl
      • Viewbox
      • Window
      • WrapPanel
    • 📚模板
      • 数据模板
      • 在代码中创建数据模板
      • 实现 IDataTemplate 接口
    • ✏️自定义控件
      • 控件类别
      • 定义属性
    • 🖱️输入
      • 路由事件
      • 剪贴板
      • 鼠标与触控设备
      • 快捷键
    • 🔑动画
      • 关键帧动画
      • 过渡
      • 页面过渡
    • 📐布局
      • 面板概述
      • Alignment、Margin 和 Padding
      • 创建自定义面板
    • 📦发布/分发
      • macOS
  • API 参考
    • 🗒️命名空间
      • Avalonia
      • Avalonia.Animation
        • Avalonia.Animation.Easings
        • Avalonia.Animation.Animators
      • Avalonia.Collections
      • Avalonia.Controls
      • Avalonia.Data
        • Avalonia.Data.Core.Plugins
        • Avalonia.Data.Core
        • Avalonia.Data.Converters
      • Avalonia.Diagnostics
      • Avalonia.Dialogs
  • 指南
    • 🐣基础
      • XAML 介绍
      • Code-behind
      • MVVM 架构
      • 在UI线程上操作
    • 🤿深入
      • 在树莓派上运行你的应用
      • 在树莓派上运行你的应用(使用Raspbian Lite)
      • ReactiveUI
        • 视图激活机制
        • 路由
        • 数据持久化
        • 绑定到 Sorted/Filtered 数据
    • 👩‍💻👩💻 开发人员指南
      • 🏭从源代码中构建 Avalonia
      • Avalonia 与 WPF 和 UWP 之间的比较
      • Debugging Previewer
      • Debugging the XAML compiler
      • macOS 开发
      • Release Process
      • 维护稳定的分支
  • 教程
    • 📋待办事项应用
    • 📻音乐商店应用
    • 🕸️在浏览器中运行
    • 📱为移动设备开发
  • 杂项
    • 👪社区
    • 🖥️WPF 开发者建议
    • 📋正在使用 Avalonia 的项目
    • ❔常见问题
由 GitBook 提供支持
在本页
  • Customizing the item display
  • Containers
  • Selection
  • SelectionMode
  • SelectedIndex
  • SelectedItem
  • Selection
  • SelectedItems
  • Preventing Horizontal Scrolling
  • Source code

这有帮助吗?

在GitHub上编辑
  1. 文档
  2. 控件

ListBox

The ListBox is an ItemsControl which displays items in a multi-line list box and allows individual selection.

The items to display in the ListBox are specified using the Items property. This property will often be bound to a collection on the control's DataContext:

<ListBox Items="{Binding MyItems}"/>

Customizing the item display

You can customize how an item is displayed by specifying an ItemTemplate. For example to display each item inside a red border with rounded corners:

<ListBox Items="{Binding MyItems}">
    <ListBox.ItemTemplate>
        <DataTemplate>
            <Border Background="Red" CornerRadius="4" Padding="4">
                <TextBlock Text="{Binding}"/>
            </Border>
        </DataTemplate>
    </ListBox.ItemTemplate>
</ListBox>

Containers

Each item displayed in a ListBox will be wrapped in a ListBoxItem - this is called the container. The container hosts the content specified in the ItemTemplate but it is not part of the ItemTemplate itself. It is the container that contains the logic for displaying selected items.

Sometimes you will want to customize the container itself. You can do this by including a style targeting ListBoxItem in the ListBox:

<ListBox Items="{Binding Items}">
    <ListBox.Styles>
        <!-- Give the ListBoxItems a fixed with of 100 and right-align them -->
        <Style Selector="ListBoxItem">
            <Setter Property="Width" Value="100"/>
            <Setter Property="HorizontalAlignment" Value="Right"/>
        </Style>
    </ListBox.Styles>
<ListBox>

In WPF and UWP this is done via the ItemContainerStyle - this property does not exist in Avalonia; you should use the method outlined above.

Selection

There are several properties related to selection on ListBox:

It is recommended that you only bind one of the SelectedIndex, SelectedItem, SelectedItems or Selection properties.

SelectionMode

Controls the type of selection that can be made on the ListBox:

Property
Description

Single

Only a single item can be selected (default)

Multiple

Multiple items can be selected

Toggle

Item selection can be toggled by tapping/spacebar. When not enabled, shift or ctrl must be used to select multiple items

AlwaysSelected

An item will always be selected as long as there are items to select.

These values can be combined, e.g.:

<ListBox SelectionMode="Multiple,Toggle">

SelectedIndex

Exposes the index of the selected item, or in the case of multiple selection the first selected item. You will often want to bind this to a view model if your list SelectionMode is set to Single.

<ListBox SelectedIndex="{Binding SelectedIndex}">
public MyViewModel : ReactiveObject
{
    int selectedIndex;

    public int SelectedIndex
    {
        get => selectedIndex;
        set => this.RaiseAndSetIfChanged(ref selectedIndex, value);
    }
}

By default bindings to this property are two-way.

SelectedItem

Exposes the selected item in the Items collection, or in the case of multiple selection the first selected item. You will often want to bind this to a view model if your list SelectionMode is set to Single.

<ListBox SelectedItem="{Binding SelectedItem}">
public MyViewModel : ReactiveObject
{
    MyItem selectedItem;

    public MyItem SelectedItem
    {
        get => selectedItem;
        set => this.RaiseAndSetIfChanged(ref selectedItem, value);
    }
}

By default bindings to this property are two-way.

Do not bind to this property if your Items collection contains duplicates as it is impossible to distinguish between duplicate values.

Selection

The Selection property exposes an ISelectionModel object with various methods to track multiple selected items. You can create a SelectionModel object in your view model and bind it to this property and subsequently control the selection from your view model.

ISelectionModel is optimized for large collections. Because of this it is recommended that you use this property in preference to SelectedItems for performance reasons.

Once Selection is bound to a SelectionModel, SelectedItems will no longer function.

SelectionModel also exposes batching functionality through its Update() method and a SelectionChanged event which details exactly which items have been selected and deselected.

<ListBox Items="{Binding Items}" Selection="{Binding Selection}">
public class MyViewModel
{
    public MyViewModel()
    {
        Items = CreateItems();

        // SelectionModel.Source can be set to Items here, or if it is left null it will be set by
        // the `ListBox` when bound.
        Selection = new SelectionModel();
        Selection.SelectionChanged += SelectionChanged;

        // Select item 10 in Items.
        Selection.Select(10);
    }

    public ObservableCollection<MyItem> Items { get; }
    public SelectionModel Selection { get; }

    // A method bound to e.g. a button which will select the first 100 items.
    public void SelectFirst100() => Selection.SelectRange(0, 99);

    // Switch to single selection via the view model.
    public void SwitchToSingleSelect() => Selection.SingleSelect = true;

    void SelectionChanged(object sender, SelectionModelSelectionChangedEventArgs e)
    {
        // ... handle selection changed
    }
}

By default bindings to this property are one-way.

SelectedItems

This property holds the selected items in an IList. It can be bound to any list that implements IList but it will usually be bound to a collection which also implements INotifyCollectionChanged such as ObservableCollection<>.

For various reasons the performance of SelectedItems can be very poor, particularly on large collections. It is recommended that you use the Selection property instead.

<ListBox SelectedItems="{Binding SelectedItems}">
public MyViewModel : ReactiveObject
{
    public ObservableCollection<MyItem> SelectedItems { get; } = new ObservableCollection<MyItem>();
}

Preventing Horizontal Scrolling

By default if an item is too wide to display in the ListBox, a horizontal scrollbar will be displayed. If instead you want items to be constrained to the width of the ListBox (for example if you want wrapping text in the items) you can disable the horizontal scrollbar by setting ScrollViewer.HorizontalScrollBarVisibility="Disabled".

<ListBox Items="{Binding MyItems}" Width="250" ScrollViewer.HorizontalScrollBarVisibility="Disabled">
    <ListBox.ItemTemplate>
        <DataTemplate>
            <Border Background="Red" CornerRadius="4" Padding="4">
                <TextBlock Text="{Binding}" TextWrapping="Wrap"/>
            </Border>
        </DataTemplate>
    </ListBox.ItemTemplate>
</ListBox>

Source code

上一页LayoutTransformControl下一页MaskedTextBox

最后更新于2年前

这有帮助吗?

🧰
ListBox.cs