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-07-05feign默认connecttimeout和readtimeout是多少-icode9专业技术文章分享
- 2024-07-05idea控制台,日志太多,导致部分想看得日志被刷走 搜不到-icode9专业技术文章分享
- 2024-07-05The server selected protocol version Tls10 is not accepted by client preferences [TLs12]-icode9专业技术文章分享
- 2024-07-05怎么清理项目缓存-icode9专业技术文章分享
- 2024-07-04安装 Eyoucms详细图文教程-icode9专业技术文章分享
- 2024-07-04ueditor 复制文章时,图片的链接是一个下载图片地址,该如何处理?-icode9专业技术文章分享
- 2024-07-04怎样判断host有没有对wordpress有缓存呢-icode9专业技术文章分享
- 2024-07-04具有编译功能的系统make后,无法ssh连接-icode9专业技术文章分享
- 2024-07-04make后如何升级ssh-icode9专业技术文章分享
- 2024-07-03微信支付提示下单账户与支付账户不一致-icode9专业技术文章分享