wpf基础

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可以用于显示数据绑定的结果。如果ListBoxItemsSource属性绑定到一个项目集合,每个ListBoxItem可以绑定到集合中的单个项目

选择和事件:当用户点击ListBoxItem时,可以触发MouseLeftButtonUp事件,这可以用来处理用户的选择

样式和模板:你可以为ListBoxItem定义样式和模板,以自定义其外观

样式和模板:你可以为ListBoxItem定义样式和模板,以自定义其外观

键盘导航:ListBoxItem支持键盘导航,允许用户使用键盘在项目之间移动和选择。

性能优化:对于包含大量项目的ListBox,建议使用VirtualizingStackPanel作为ListBoxItemsPanelTemplate,以优化性能:

选择模式

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; }
​
    }
}

  • 3
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值