listbox +CheckBox
2021/7/19 6:04:46
本文主要是介绍listbox +CheckBox,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!
简要介绍
目前为止最强WPF进阶教程WPF进阶教程
现阶段对部分概念依旧不怎么理解,但是此教程能跟着解决部分自制控件的问题
解决方案
<Window x:Class="WpfApp6.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:WpfApp6" mc:Ignorable="d" Title="MainWindow" Height="450" Width="800"> <Window.Resources> <Style x:Key="checkboxListStyle" TargetType="{x:Type ListBox}"> <Setter Property="SelectionMode" Value="Multiple"></Setter> <Setter Property="ItemContainerStyle" > <Setter.Value> <Style TargetType="{x:Type ListBoxItem}"> <Setter Property="Margin" Value="2"></Setter> <Setter Property="Template"> <Setter.Value > <ControlTemplate TargetType="{x:Type ListBoxItem}"> <CheckBox IsChecked="{Binding Path=IsSelected,RelativeSource={RelativeSource TemplatedParent},Mode=TwoWay}"> <ContentPresenter></ContentPresenter> </CheckBox> </ControlTemplate> </Setter.Value> </Setter> </Style> </Setter.Value> </Setter> </Style> </Window.Resources> <Grid> <Grid.RowDefinitions> <RowDefinition></RowDefinition> <RowDefinition Height="auto"></RowDefinition> </Grid.RowDefinitions> <ListBox Style="{StaticResource checkboxListStyle}" Name="listProducts" ></ListBox> <Button Grid.Row="1" Name="btn" Click="btn_Click">get Selected item</Button> </Grid> </Window>
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; 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 WpfApp6 { /// <summary> /// MainWindow.xaml 的交互逻辑 /// </summary> public partial class MainWindow : Window { public MainWindow() { InitializeComponent(); List<student> students = new List<student> { new student(){ModelName="校长"}, new student(){ModelName="小王"} }; this.listProducts.ItemsSource = students; this.listProducts.DisplayMemberPath = "ModelName"; } private void btn_Click(object sender, RoutedEventArgs e) { if (listProducts.SelectedItems.Count > 0) { string items = "you Selected:"; foreach (student item in listProducts.SelectedItems) { items += "\n" + item.ModelName; } MessageBox.Show(items); } } private void allbtn_Click(object sender, RoutedEventArgs e) { this.listProducts.SelectAll(); } private void clearbtn_Click(object sender, RoutedEventArgs e) { this.listProducts.UnselectAll(); } public class student { public string ModelName { get; set; } public bool? IsSelected { get; set; } } } }
这篇关于listbox +CheckBox的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!
- 2024-12-23新能源车企如何通过CRM工具优化客户关系管理,增强客户忠诚度与品牌影响力
- 2024-12-23原创tauri2.1+vite6.0+rust+arco客户端os平台系统|tauri2+rust桌面os管理
- 2024-12-23DevExpress 怎么实现右键菜单(Context Menu)显示中文?-icode9专业技术文章分享
- 2024-12-22怎么通过控制台去看我的页面渲染的内容在哪个文件中呢-icode9专业技术文章分享
- 2024-12-22el-tabs 组件只被引用了一次,但有时会渲染两次是什么原因?-icode9专业技术文章分享
- 2024-12-22wordpress有哪些好的安全插件?-icode9专业技术文章分享
- 2024-12-22wordpress如何查看系统有哪些cron任务?-icode9专业技术文章分享
- 2024-12-21Svg Sprite Icon教程:轻松入门与应用指南
- 2024-12-20Excel数据导出实战:新手必学的简单教程
- 2024-12-20RBAC的权限实战:新手入门教程