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

上一篇:从源码角度谈谈open_files_limit的生成逻辑及"Too many open files"的解决思路


下一篇:2021-7-19 Frequency of the Most Frequent Element