WPF 控件专题 ListBox 控件详解

1、ListBox 介绍

    ListBox 列表控件。

    ListBox 是一个 ItemsControl,这意味着它可以包含任何类型的对象的集合 (,例如字符串、图像或面板) 。

    一个 ListBox 中的多个项是可见的,与仅 ComboBox具有所选项可见的项不同,除非 IsDropDownOpen 属性为 true。 该 SelectionMode 属性确定一次是否可以选择多个项 ListBox 。

**************************************************************************************************************

2、常用属性介绍

    FontFamily:字体系列;    FontSize:字体大小;    FontStretch:字体在屏幕上紧缩或加宽的程度;FontWeight:字体粗细;
    
    Background:背景;    BorderBrush:边框颜色;    BorderThickness:边框宽度;    Foreground:前景色;
    
    Width/Height:宽度/高度;    Name:元素标识名称;    IsEnabled:使能,是否可用;    Margin:外边距;
    
    Opacity:透明度;    Visibility:可见性;    IsVisible:是否可见;    FlowDirection:其子元素的流动方向;
    
    LayoutTransform:在执行布局时应该应用于此元素的图形转换方式。    RenderTransform:元素的呈现位置的转换信息;
    
    RenderTransformOrigin:由RenderTransform声明的任何可能呈现转换的中心点,相对于元素的边界。
    
    HorizontalAlignment/VerticalAlignment:在父元素中组合此元素时所应用的水平对齐特征/垂直对齐特征。
    
    HorizontalContentAlignment/VerticalContentAlignment:控件内容的水平对齐方式/垂直对齐方式。

    Items:获取用于生成 ItemsControl 的内容的集合。
    
    ItemsSource:获取或设置用于生成 ItemsControl 的内容的集合。

    SelectedIndex:获取或设置当前选择中第一项的索引,如果选择为空,则返回负一(-1)。

    SelectedItem:获取或设置当前选择中的第一项,或者,如果选择为空,则返回 null。

    SelectedItems:获取当前选定的项。

    SelectedValue:获取或设置通过使用 SelectedItem 而获取的 SelectedValuePath 的值。

    SelectedValuePath:获取或设置用于从 SelectedValue 获取 SelectedItem 的路径。

    SelectionMode:获取或设置 ListBox 的选择行为。

    SnapsToDevicePixels:获取或设置一个值,该值确定在呈现过程中,此元素的呈现是否应使用特定于设备的像素设置。

**************************************************************************************************************

3、具体示例代码

    XAML代码

<StackPanel>
	<TextBox Name="tb" Width="180" Height="30"></TextBox>
	<ListBox Name="lb" Width="140" SelectionChanged="PrintText" SelectionMode="Single" Margin="10">
		<ListBoxItem>Item 1</ListBoxItem>
		<ListBoxItem>Item 2</ListBoxItem>
		<ListBoxItem>Item 3</ListBoxItem>
		<ListBoxItem>Item 4</ListBoxItem>
		<ListBoxItem>Item 5</ListBoxItem>
		<ListBoxItem>Item 6</ListBoxItem>
		<ListBoxItem>Item 7</ListBoxItem>
		<ListBoxItem>Item 8</ListBoxItem>
		<ListBoxItem>Item 9</ListBoxItem>
		<ListBoxItem>Item 10</ListBoxItem>
	</ListBox>
</StackPanel>

    后端代码

private void PrintText(object sender, SelectionChangedEventArgs e)
{
	ListBoxItem lbi = ((sender as ListBox).SelectedItem as ListBoxItem);
	tb.Text = "   You selected " + lbi.Content.ToString() + ".";
}

**************************************************************************************************************

4、效果图

**************************************************************************************************************

5、自定义样式

<Style x:Key="listBoxItemStyle" TargetType="{x:Type ListBoxItem}">
	<Setter Property="Template">
		<Setter.Value>
			<ControlTemplate TargetType="{x:Type ListBoxItem}">
				<Border  x:Name="IconBorder" Background="#555a64" CornerRadius="4" BorderThickness="0" Margin="5 5">
					<ContentPresenter />
				</Border>
				<ControlTemplate.Triggers>
					<Trigger Property="IsSelected" Value="true">
						<Setter TargetName="IconBorder" Property="BitmapEffect">
							<Setter.Value>
								<OuterGlowBitmapEffect GlowColor="Transparent" GlowSize="5" />
							</Setter.Value>
						</Setter>
					</Trigger>
				</ControlTemplate.Triggers>
			</ControlTemplate>
		</Setter.Value>
	</Setter>
</Style>
<ListBox x:Name="MyListBox" Background="#2d323c" Width="200" Margin="20" 
		 ItemContainerStyle="{StaticResource listBoxItemStyle}" FocusVisualStyle="{x:Null}" ItemsSource="{Binding ListBoxDatas}">
	<ListBox.Template>
		<ControlTemplate>
			<StackPanel Background="White" IsItemsHost="True"></StackPanel>
		</ControlTemplate>
	</ListBox.Template>
	<ListBox.ItemTemplate>
		<DataTemplate>
			<StackPanel Orientation="Horizontal" HorizontalAlignment="Left">
				<CheckBox IsChecked="{Binding IsChecked}" Height="20" Width="20"  VerticalAlignment="Center" Margin="10,5"/>
				<Ellipse Height="14" Width="14" Fill="{Binding Color}"  VerticalAlignment="Center" Margin="5"/>
				<TextBlock Text="{Binding NameText}" VerticalAlignment="Center" Margin="5" FontSize="16"/>
			</StackPanel>
		</DataTemplate>
	</ListBox.ItemTemplate>
</ListBox>
public partial class MainWindow : Window, System.ComponentModel.INotifyPropertyChanged
{
	public MainWindow()
	{
		InitializeComponent();
		this.DataContext = this;

		ListBoxDatas.Add(new ListBoxData() { Color = Brushes.Red, IsChecked = false, NameText = "张涛涛1" });
		ListBoxDatas.Add(new ListBoxData() { Color = Brushes.Green, IsChecked = true, NameText = "张涛涛2" });
		ListBoxDatas.Add(new ListBoxData() { Color = Brushes.Blue, IsChecked = false, NameText = "张涛涛3" });
		ListBoxDatas.Add(new ListBoxData() { Color = Brushes.Orange, IsChecked = true, NameText = "张涛涛4" });
		ListBoxDatas.Add(new ListBoxData() { Color = Brushes.Yellow, IsChecked = false, NameText = "张涛涛5" });
		ListBoxDatas.Add(new ListBoxData() { Color = Brushes.YellowGreen, IsChecked = true, NameText = "张涛涛6" });
	}
	
	private List<ListBoxData>listBoxDatas = new List<ListBoxData>();
	public List<ListBoxData> ListBoxDatas
	{
		get { return listBoxDatas; }
		set { listBoxDatas = value; OnPropertyChanged("ListBoxDatas"); }
	}


	public event System.ComponentModel.PropertyChangedEventHandler PropertyChanged;
	protected void OnPropertyChanged(string propertyName)
	{
		if (PropertyChanged != null)
		{
			PropertyChanged(this, new System.ComponentModel.PropertyChangedEventArgs(propertyName));
		}
	}
}

public class ListBoxData
{
	public bool IsChecked { get; set; }
	public Brush Color { get; set; }
	public string NameText { get; set; }
}

**************************************************************************************************************

效果图

 通过Template改变列表的布局,如使用WrapPanel进行布局;或者ItemsPanel也可

<ListBox.Template>
	<ControlTemplate>
		<WrapPanel  Background="#2d323c" IsItemsHost="True"/>
	</ControlTemplate>
</ListBox.Template>

效果图

**************************************************************************************************************

6、总结和扩展

    IsItemsHost=true表示子元素将显示在此容器中。

    第5点,演示了自定义的样式和Data绑定功能。

    该 SelectionMode 属性确定用户可以一次选择的项数。 可以将属性设置为 Single (默认) , Multiple或 Extended。 下表描述了这些枚举值的行为。
        Single:用户一次只能选择一项。
        Multiple:用户可以选择多个项而无需按下修改键。
        Extended:用户可以通过按住 Ctrl 键并单击项来选择多个连续项,同时按住 SHIFT 键或非连续项。

**************************************************************************************************************

  • 1
    点赞
  • 30
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
WPF.NET 6.0中的ListBox控件是一种用于显示列表数据的控件。它可以在界面上显示多个项,并允许用户选择其中的一个或多个项。引用\[1\]中列举了一些常见的WPF控件,其中包括ListBox。 在VC6中创建一个MFC AppWizard (exe)项目,并选择dialog based模板后,可以在dialog中插入一个ListBox控件。可以通过修改ListBox的属性来改变其样式,比如将Styles中的Owner draw改为Fixed或Variable。\[2\] 在XAML中,可以使用附加属性来控制ListBox的行为。例如,可以使用Trigger来判断附加属性PasswordLength是否等于0,如果是,则显示水印。具体的代码可以参考引用\[3\]中的示例。 总结起来,WPF.NET 6.0中的ListBox控件是一种用于显示列表数据的控件,可以通过修改属性来改变其样式,并且可以使用附加属性来控制其行为。 #### 引用[.reference_title] - *1* *3* [WPF 基础控件之 PasswordBox 样式](https://blog.csdn.net/zls365365/article/details/124642528)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^insertT0,239^v3^insert_chatgpt"}} ] [.reference_item] - *2* [改变ListBox中文本的颜色以及其他](https://blog.csdn.net/wu5318/article/details/11381417)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^insertT0,239^v3^insert_chatgpt"}} ] [.reference_item] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值