ListBox
在ListBox中打印十个项目
using System.Collections.Generic;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
namespace _7._29dayWPF02
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
for (int i = 0; i < 10; i++)
{
list.Items.Add(new ListBoxItem()
{
Content = new TextBlock()
{
Text = i.ToString()
}
});
}
}
}
}
<Window x:Class="_7._29dayWPF02.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:_7._29dayWPF02"
mc:Ignorable="d"
Title="MainWindow" Height="450" Width="800">
<Grid>
<ListBox x:Name="list"/>
</Grid>
</Window>
在WPF(Windows Presentation Foundation)中,ListBox
是一个用于显示项目列表的控件,允许用户从中选择一个或多个项。ListBox
支持数据绑定,样式自定义,以及多种选择模式。
以下是一些关于WPF中ListBox
的关键点:
基本用法
在XAML中,你可以这样定义一个ListBox
:
<ListBox x:Name="MyListBox" />
添加数据项
你可以通过代码在后台添加数据项:
MyListBox.Items.Add("Item 1");
MyListBox.Items.Add("Item 2");
// 等等...
数据绑定
WPF的ListBox
支持数据绑定,这意味着你可以将一个数据源绑定到ListBox
,然后使用ItemTemplate
来自定义每个项的显示方式:
<Window x:Class="_7._29dayWPF02.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:_7._29dayWPF02"
mc:Ignorable="d"
Title="MainWindow" Height="450" Width="800">
<Grid>
<ListBox x:Name="list">
<ListBox.ItemTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal">
<Border Width="10" Height="10" Background="red"></Border>
<TextBlock Margin="10,0" Text="red"></TextBlock>
</StackPanel>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
</Grid>
</Window>
using System.Collections.Generic;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
namespace _7._29dayWPF02
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
List<int> test = new List<int>();
for (int i = 0; i < 10; i++)
{
test.Add(i);
}
list.ItemsSource = test;
}
}
}
ListBoxItem
ListBoxItem
是一个控件,代表在ListBox
控件中的单个项目。每个ListBoxItem
代表ListBox
中的一行,可以包含文本、图像或其他控件。
以下是ListBoxItem
的一些关键特性和用法:
基本定义:在XAML中,你可以直接定义ListBoxItem
作为ListBox
的子元素
内容定义:ListBoxItem的内容可以是任何有效的WPF内容,包括文本、图像或其他控件
数据绑定:ListBoxItem
可以用于显示数据绑定的结果。如果ListBox
的ItemsSource
属性绑定到一个项目集合,每个ListBoxItem
可以绑定到集合中的单个项目
选择和事件:当用户点击ListBoxItem
时,可以触发MouseLeftButtonUp
事件,这可以用来处理用户的选择
样式和模板:你可以为ListBoxItem
定义样式和模板,以自定义其外观
样式和模板:你可以为ListBoxItem
定义样式和模板,以自定义其外观
键盘导航:ListBoxItem
支持键盘导航,允许用户使用键盘在项目之间移动和选择。
性能优化:对于包含大量项目的ListBox
,建议使用VirtualizingStackPanel
作为ListBox
的ItemsPanelTemplate
,以优化性能:
选择模式
ListBox
支持单选和多选模式,这可以通过SelectionMode
属性来设置:
<ListBox x:Name="MyListBox" SelectionMode="Single" />
<!-- 或者多选 -->
<ListBox x:Name="MyListBox" SelectionMode="Multiple" />
选择事件
当用户选择一个或多个项时,你可以处理SelectionChanged
事件:
MyListBox.SelectionChanged += MyListBox_SelectionChanged;
private void MyListBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
// 处理选中项
}
滚动事件
如果ListBox
中的数据很多,你可以处理ScrollChangedEventArgs
事件来响应滚动行为:
MyListBox.ScrollChanged += MyListBox_ScrollChanged;
private void MyListBox_ScrollChanged(object sender, ScrollChangedEventArgs e)
{
// 处理滚动事件
}
虚拟化
对于包含大量数据项的ListBox
,启用虚拟化可以提高性能:
<ListBox x:Name="MyListBox" VirtualizingStackPanel.IsVirtualizing="True" VirtualizingStackPanel.VirtualizationMode="Recycling" />
虚拟化意味着WPF只会生成当前可视区域内的项,当用户滚动时,旧的项会被新的项替换,而不是一次性渲染所有项。
样式和模板
你可以自定义ListBox
的样式和控件模板,以改变其外观:
<Window x:Class="_7._29dayWPF02.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:_7._29dayWPF02"
mc:Ignorable="d"
Title="MainWindow" Height="450" Width="800">
<Grid>
<ListBox x:Name="list">
<ListBox.ItemTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal">
<Border Width="10" Height="10" Background="{Binding code}"></Border>
<TextBlock Margin="10,0" Text="{Binding Name}"></TextBlock>
</StackPanel>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
</Grid>
</Window>
using System.Collections.Generic;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
namespace _7._29dayWPF02
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
List<Color> test = new List<Color>();
test.Add(new Color() { code = "#FF69B4", Name = "HotPink" });
test.Add(new Color() { code = "#FF1493", Name = "DeepPink" });
test.Add(new Color() { code = "#00F5FF", Name = "Turquoise1" });
test.Add(new Color() { code = "#54FF9F", Name = "SeaGreen1" });
//HotPink 255 105 180 #FF69B4
//DeepPink 255 20 147 #FF1493
//Turquoise1 0 245 255 #00F5FF
//SeaGreen1 84 255 159 #54FF9F
list.ItemsSource = test;
}
}
public class Color
{
public string code { get; set; }
public string Name { get; set; }
}
}
数据模板在许多控件中都有使用
<Window x:Class="_7._29dayWPF02.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:_7._29dayWPF02"
mc:Ignorable="d"
Title="MainWindow" Height="450" Width="800">
<Grid>
<DataGrid x:Name="grid" AutoGenerateColumns="False"
CanUserAddRows="False"
>
<DataGrid.Columns>
<DataGridTextColumn Binding="{Binding code}" Header="Code"/>
<DataGridTextColumn Binding="{Binding Name}" Header="Name"/>
<DataGridTemplateColumn Header="操作">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal">
<Border Width="10" Height="10" Background="{Binding code}"/>
<TextBlock Margin="10" Text="{Binding Name}"/>
</StackPanel>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
</DataGrid.Columns>
</DataGrid>
</Grid>
</Window>
using System.Collections.Generic;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
namespace _7._29dayWPF02
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
List<Color> test = new List<Color>();
test.Add(new Color() { code = "#FF69B4", Name = "HotPink" });
test.Add(new Color() { code = "#FF1493", Name = "DeepPink" });
test.Add(new Color() { code = "#00F5FF", Name = "Turquoise1" });
test.Add(new Color() { code = "#54FF9F", Name = "SeaGreen1" });
//HotPink 255 105 180 #FF69B4
//DeepPink 255 20 147 #FF1493
//Turquoise1 0 245 255 #00F5FF
//SeaGreen1 84 255 159 #54FF9F
grid.ItemsSource = test;
}
}
public class Color
{
public string code { get; set; }
public string Name { get; set; }
}
}