ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

listbox +CheckBox

2021-07-19 06:00:06  阅读:219  来源: 互联网

标签:CheckBox Windows System ModelName listProducts using listbox public


简要介绍

目前为止最强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; }

        }
    }
}


标签:CheckBox,Windows,System,ModelName,listProducts,using,listbox,public
来源: https://www.cnblogs.com/lileiblog/p/15028577.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有