网络营销人员招聘信息,wordpress国内优化 墙,江西网站备案要求,一小时学会网站建设为 ListView 控件的内容指定视图模式的方法#xff0c;参考官方文档。
ComboBox 样式和模板
案例说明#xff1a;通过checkBox和ComboBox的组合方式实现下拉窗口的多选方式#xff0c;同时说明了ListView中Items项目的两种绑定方式.
示例#xff1a;
设计样式 Xaml代码…为 ListView 控件的内容指定视图模式的方法参考官方文档。
ComboBox 样式和模板
案例说明通过checkBox和ComboBox的组合方式实现下拉窗口的多选方式同时说明了ListView中Items项目的两种绑定方式.
示例
设计样式 Xaml代码
Window x:ClassComboBox自定义多选.MainWindowxmlnshttp://schemas.microsoft.com/winfx/2006/xaml/presentationxmlns:xhttp://schemas.microsoft.com/winfx/2006/xamlxmlns:dhttp://schemas.microsoft.com/expression/blend/2008xmlns:mchttp://schemas.openxmlformats.org/markup-compatibility/2006xmlns:localclr-namespace:ComboBox自定义多选xmlns:hchttps://handyorg.github.io/handycontrol mc:IgnorabledTitleMainWindowHeight450Width800BackgroundDarkGrayGridGrid.ColumnDefinitionsColumnDefinition Width300/ColumnDefinitionColumnDefinition/ColumnDefinition/Grid.ColumnDefinitionsStackPanel!-- 定义多选ComboBox --ComboBox NamemultiSelectComboBoxWidth200Height30HorizontalAlignmentLeft IsEditableTrueStaysOpenOnEditTrueIsReadOnlyTrueText多选列表Margin10!-- 定义ComboBox的ItemTemplate包含一个CheckBox --ComboBox.ItemTemplateDataTemplateCheckBox Content{Binding Name}IsChecked{Binding IsSelected, ModeTwoWay} //DataTemplate/ComboBox.ItemTemplate/ComboBox!-- 按钮显示所选项目 --Button Content查看选择了什么选项Width170Height30VerticalAlignmentTopHorizontalAlignmentLeftMargin10ClickShowSelectedOptions_Click /TextBlock NameSelectItemsMargin10/TextBlock/StackPanelWrapPanel Grid.Column1ListView NameStudentList Margin10ListView.ViewGridViewGridViewColumn Header姓名 Width200DisplayMemberBinding{Binding Name}/GridViewColumnGridViewColumn Header年龄Width200DisplayMemberBinding{Binding Age}/GridViewColumn/GridView/ListView.View/ListViewButton NameMode1Margin10HorizontalAlignmentLeftContent方式一ClickMode1_Click/ButtonButton NameMode2Margin10HorizontalAlignmentLeftContent方式二ClickMode2_Click/Button/WrapPanel/Grid
/WindowCS代码
using System.Collections.ObjectModel;
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 ComboBox自定义多选
{/// summary/// Interaction logic for MainWindow.xaml/// /summarypublic partial class MainWindow : Window{public ObservableCollectionStudent Items { get; set; }public MainWindow(){InitializeComponent();// 初始化选项集合Items new ObservableCollectionStudent{new Student { Name 张三, Age 20},new Student { Name 李四, Age 21},new Student { Name 王五, Age 22},new Student { Name 赵六, Age 23}};// 将Items集合绑定到ComboBox的ItemsSourcemultiSelectComboBox.ItemsSource Items; }// 显示已选择的选项private void ShowSelectedOptions_Click(object sender, RoutedEventArgs e){// 获取所有IsSelected为true的项目var selectedItems Items.Where(item item.IsSelected).Select(item item.Name).ToList();// 显示选择的项目SelectItems.Text 你选择了: string.Join(, , selectedItems);}// 数据项类public class Student{public string? Name { get; set; }public string? Age { get; set; }public bool IsSelected { get; set; }}private void Mode1_Click(object sender, RoutedEventArgs e){StudentList.Items.Clear();// 初始化选项集合Items new ObservableCollectionStudent{new Student { Name 张三, Age 20},new Student { Name 李四, Age 21},new Student { Name 王五, Age 22},new Student { Name 赵六, Age 23}};// 将Items集合绑定到ListView的ItemsSourceStudentList.ItemsSource Items;}private void Mode2_Click(object sender, RoutedEventArgs e){StudentList.ItemsSource null;StudentList.Items.Clear();StudentList.Items.Add(new Student { Name 孙悟空, Age 10000 });StudentList.Items.Add(new Student { Name 悟能, Age 5000 });StudentList.Items.Add(new Student { Name 悟净, Age 3000 });StudentList.Items.Add(new Student { Name 唐僧, Age 30 });}}
}
使用效果展示
启动页面 点击“方式一” 点击“方式二” 查看多选框的下拉菜单 选择两个项目 点击“查看选择了什么选项”