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 " 12 true ));
            
this .persons.Add( new  Person( " Rose " 21 false ));
            
this .persons.Add( new  Person( " Jack " 12 true ));
            
this .persons.Add( new  Person( " Jenny " 21 false ));
            
this .persons.Add( new  Person( " Tom " 12 true ));
            
this .persons.Add( new  Person( " Alma " 21 false ));
        }

        
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 {  get set ; }
        
public  Int32 Age {  get set ; }
        
public  String Sex {  get set ; }
    }

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


}

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
WPF(Windows Presentation Foundation)是一种用于创建 Windows 应用程序的框架,它提供了丰富的图形和用户界面功能。实现像 XMind 这样的思维导图工具的过程如下: 1. 数据结构设计:首先需要设计思维导图的数据结构,包括节点(节点可能有不同的类型,如主题、子主题、注释等)、连接线等。可以使用树状结构或图结构来表示思维导图的组织关系。 2. 界面设计:使用 WPF 的图形和用户界面功能来设计思维导图的界面。可以使用画布(Canvas)来承载节点和连接线,通过鼠标事件来实现节点拖拽、连线等功能。可以为节点和连接线定义样式和模板,以美化界面和提供更多交互效果。 3. 数据绑定:将思维导图的数据模型与界面进行绑定,使得界面能够动态展示数据的变化。可以使用 WPF 的数据绑定机制,将节点的属性绑定到界面控件上,当属性值发生变化时,界面会自动更新。 4. 布局和自动排版:思维导图中的节点可能会很多,因此需要实现自动排版来保证节点的布局整齐美观。可以使用 WPF 的布局控件如网格(Grid)、堆栈面板(StackPanel)等进行节点的布局,并根据节点之间的关系自动调整节点的位置和大小。 5. 导出和导入:实现将思维导图保存为文件或导入文件的功能,可以使用 WPF 的文件操作功能来实现。可以将思维导图保存为 XML、JSON 或其他格式,并提供打开、保存功能供用户使用。 通过以上步骤,就可以使用 WPF 实现类似 XMind 的思维导图工具。当然,具体的实现过程和功能细节还需要根据实际需求进行具体设计和开发。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值