首页 > 其他 > 详细

listbox +CheckBox

时间:2021-07-19 09:46:06      阅读:19      评论:0      收藏:0      [点我收藏+]

简要介绍

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

原文:https://www.cnblogs.com/lileiblog/p/15028577.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!