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;
            }
        }


    }
  • 相关阅读:
    Python基础教程【读书笔记】
    Python基础教程【读书笔记】
    Python基础教程【读书笔记】
    Python基础教程【读书笔记】
    JS实现焦点图轮播效果
    JQuery+CSS3实现Ajax加载时loading效果
    JQuery实现锚点平滑滚动
    CSS3之嵌入Web字体
    HTML5本地存储
    impress.js初体验——前端装X利器
  • 原文地址:https://www.cnblogs.com/xh831213/p/1772064.html
Copyright © 2011-2022 走看看