zoukankan      html  css  js  c++  java
  • wpf 实现check box list

    xaml

    <Window x:Class="TestCheckListView.Window1"
        xmlns
    ="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x
    ="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:l
    ="clr-namespace:TestCheckListView"
        Title
    ="Window1" Height="398" Width="400">
        
    <Window.Resources>
            
    <l:CheckBoxVisibilityConverter x:Key="CheckBoxVisibilityConverter"/>
            
    <GridView x:Key="gridview">
                
    <GridViewColumn Header="Name">
                    
    <GridViewColumn.CellTemplate>
                        
    <DataTemplate>
                            
    <StackPanel Orientation="Horizontal">
                                
    <CheckBox IsChecked="{Binding Path=IsSelected, RelativeSource={RelativeSource AncestorType={x:Type ListViewItem}}}">
                                    
    <CheckBox.Visibility>
                                        
    <MultiBinding Converter="{StaticResource CheckBoxVisibilityConverter}">
                                            
    <Binding Path="IsMouseOver" RelativeSource="{RelativeSource AncestorType={x:Type ListViewItem}}"/>
                                            
    <Binding Path="IsSelected" RelativeSource="{RelativeSource AncestorType={x:Type ListViewItem}}"/>
                                        
    </MultiBinding>
                                    
    </CheckBox.Visibility>
                                
    </CheckBox>
                                
    <TextBlock Text="{Binding Name}"/>
                            
    </StackPanel>
                        
    </DataTemplate>
                    
    </GridViewColumn.CellTemplate>
                
    </GridViewColumn>
                
    <GridViewColumn Header="Age" DisplayMemberBinding="{Binding Age}"/>
                
    <GridViewColumn Header="Sex" DisplayMemberBinding="{Binding Sex}"/>
            
    </GridView>
        
    </Window.Resources>

        
    <Grid Margin="10">
            
    <ListView Name="lv" View="{StaticResource gridview}" Margin="0,0,0,75" />
            
    <Button Height="23" HorizontalAlignment="Left" Margin="90,0,0,28" Name="button1" VerticalAlignment="Bottom" Width="75" Click="button1_Click">Button</Button>
        
    </Grid>

    </Window>

    c#

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    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;
    using System.Collections.ObjectModel;

    namespace TestCheckListView
    {
        
    /// <summary>
        
    /// Interaction logic for Window1.xaml
        
    /// </summary>
        public partial class Window1 : Window
        {
            
    public Window1()
            {
                InitializeComponent();

                
    this.lv.ItemsSource = this.persons;

                
    this.persons.Add(new Person("John"12true));
                
    this.persons.Add(new Person("Rose"21false));
                
    this.persons.Add(new Person("Jack"12true));
                
    this.persons.Add(new Person("Jenny"21false));
                
    this.persons.Add(new Person("Tom"12true));
                
    this.persons.Add(new Person("Alma"21false));
            }

            
    private ObservableCollection<Person> persons = new ObservableCollection<Person>();

            
    private void button1_Click(object sender, RoutedEventArgs e)
            {
                
    foreach (Person item in lv.SelectedItems)
                {
                    MessageBox.Show(item.Name);
                }
            }

        }

        
    public class Person
        {
            
    public Person(String name, Int32 age, Boolean bMale)
            {
                
    this.Name = name;
                
    this.Age = age;
                
    this.Sex = bMale ? "M" : "F";
            }

            
    public String Name { getset; }
            
    public Int32 Age { getset; }
            
    public String Sex { getset; }
        }

        
    public class CheckBoxVisibilityConverter : IMultiValueConverter
        {
            
    public object Convert(object[] values, Type targetType, object parameter, System.Globalization.CultureInfo culture)
            {
                
    try
                {
                    Boolean isMouseOver 
    = (Boolean)(values[0]);
                    Boolean isSelected 
    = (Boolean)(values[1]);
                    
    if (isSelected)
                        
    return Visibility.Visible;
                    
    else if (isMouseOver)
                        
    return Visibility.Visible;
                    
    else
                        
    return Visibility.Hidden;
                }
                
    catch { return Visibility.Hidden; }
            }

            
    public object[] ConvertBack(object value, Type[] targetTypes, object parameter, System.Globalization.CultureInfo culture)
            {
                
    return null;
            }
        }


    }
  • 相关阅读:
    Flask web开发之路二
    Flask web开发之路一
    英文文本挖掘预处理总结
    TF-IDF概念
    MongoDB数据库去重
    Python把两个列表合成一个字典
    网络通信协议七之ARP工作过程及工作原理解析
    Python基础爬虫
    Red and Black 模板题 /// BFS oj22063
    Alice拜年 模板题 /// 最短路Dijk oj1344
  • 原文地址:https://www.cnblogs.com/xh831213/p/1772064.html
Copyright © 2011-2022 走看看