诸位老师好。
wpf没有CheckListBox控件,我用是扩展的xctk:CheckListBox控件, 我不知道该怎样用模板绑定数据源。
xaml配置代码:<Window xmlns:my="clr-namespace:DevComponents.WPF.Controls;assembly=DevComponents.WPF.Controls"  
        x:Class="TestWpf.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:Rules="clr-namespace:TestWpf" mc:Ignorable="d" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/up-compatibility/2006" d:DesignHeight="409" SizeToContent="WidthAndHeight" d:DesignWidth="592" xmlns:xctk="http://schemas.xceed.com/wpf/xaml/toolkit">    <Grid>
        <Button Content="Button" Height="23" HorizontalAlignment="Left" Margin="187,322,0,0" Name="button1" VerticalAlignment="Top" Width="147" Click="button1_Click" />
        <xctk:CheckListBox Height="100" HorizontalAlignment="Left" Margin="30,75,0,0" Name="checkListBox1" VerticalAlignment="Top" Width="321">
            <DataTemplate>
                <Grid>
                    <Grid.ColumnDefinitions>
                        <ColumnDefinition Width="60"></ColumnDefinition>
                        <ColumnDefinition Width="400"></ColumnDefinition>
                        <ColumnDefinition Width="60"></ColumnDefinition>
                        <ColumnDefinition Width="200"></ColumnDefinition>
                    </Grid.ColumnDefinitions>
                    <TextBlock>曲目路径</TextBlock>
                    <TextBox Width="400" Text="{Binding Username}"/>
                    <TextBlock>曲目名称</TextBlock>
                    <TextBox Width="200" Text="{Binding Password}"/>                    
                </Grid>
                
            </DataTemplate>
        </xctk:CheckListBox>
    </Grid>
</Window>我想实现一个既可以绑定显示,又可以修改的。checkbox是选择删除操作用的。
后台代码
Persion.cs类public class Person
    {
        int uid;        public int Uid
        {
            get { return uid; }
            set { uid = value; }
        }
        string username;        public string Username
        {
            get { return username; }
            set { username = value; }
        }
        string password;        public string Password
        {
            get { return password; }
            set { password = value; }
        }
    }
//MainWindow.xaml.cs代码
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 DevComponents.WPF.Controls;
using Microsoft.Win32;
using System.Xml;
using System.Data;
using Xceed.Wpf.Toolkit;namespace TestWpf
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }        private void button1_Click(object sender, RoutedEventArgs e)
        {
            List<User> list = new List<User>();
            for (int i = 0; i < 20;i++ )
            {
                User user = new User();
                user.Uid =i;
                user.Username = "userName" + i;
                user.Password = "passWord" + i;                list.Add(user);
            }                      this.checkListBox1.ItemsSource = list;
        }
        
    }
}
上面代码运行出现异常。"Items collection must be empty before using ItemsSource."}
请问该如何实现我上面的功能呢。多谢了。

解决方案 »

  1.   

    1. wpf没有CheckListBox控件  
       > 这个用 ListBox,ListView,GridView 都可以自定义DataTemplate实现。2. 现在看不到 checkListBox1 具体实现也无法知道直接绑定 ItemSource 有什么问题。
      

  2.   


    非常感谢,如果用ListBox完成上述功能,怎么做呢?第一列显示一个CheckBox,后面两列,分别显示User的UserName 和PassWord的属性值,通过ListBox显示并可以修改值,CheckBox选择删除。怎么做呢?多谢。在线恭候!!
      

  3.   

    建议使用 MVVM (成熟的框架有 MVVMLight,可以通过NuGet直接安装)下面是直接使用双向binding来删除的。在User里追加一个IsSelected属性,
    删除对于 MVVM 来说,真是太爽了。下面是后台代码(其实应该把后台代码抽到 VM 里,Button_Click 应变为 Command)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;using System.Collections.ObjectModel;
    using System.ComponentModel;namespace WpfChecklistboxTest
    {
        
        /// <summary>
        /// MainWindow.xaml 的交互逻辑
        /// </summary>
        public partial class MainWindow : Window
        {
            public MainWindow()
            {
                InitializeComponent();
            }        ObservableCollection<User> Users { get; set; }        private void Window_Loaded_1(object sender, RoutedEventArgs e)
            {
                Users = new ObservableCollection<User>();
                for (var i = 0; i < 10; i++)
                { 
                    var no = i.ToString().PadLeft(3, '0');
                    Users.Add(new User { UserName = "user" + no, Password = "password" + no, IsSelected=false });
                }
                this.DataContext = Users;
            }
            // 删除操作!
            private void Button_Click_1(object sender, RoutedEventArgs e)
            {
                Users.Where(u => u.IsSelected).ToList().ForEach(u => Users.Remove(u));            
            }
        }     public class ObservableBase : INotifyPropertyChanged
        {
            public event PropertyChangedEventHandler PropertyChanged;        public virtual void RaisePropertyChanged(string propertyName)
            {
                if (PropertyChanged != null)
                {
                    PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
                }
            }
        }    public class User : ObservableBase
        {
            private string _userName;
            private string _password;
            private bool _isSelected;           public string UserName
            {
                get { return _userName; }
                set
                {
                    _userName = value;
                    RaisePropertyChanged("UserName");
                }
            }
            public string Password
            {
                get { return _password; }
                set
                {
                    _password = value;
                    RaisePropertyChanged("Password");
                }
            }
            public bool IsSelected
            {
                get { return _isSelected; }
                set
                {
                    _isSelected = value;
                    RaisePropertyChanged("IsSelected");
                }
            }
        }
    }
    Xmal 代码:<Window x:Class="WpfChecklistboxTest.MainWindow"
            xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
            xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
            Title="MainWindow" Height="350" Width="525" Loaded="Window_Loaded_1">
        <Grid>
            <ListBox HorizontalAlignment="Left" Height="247" Margin="10,10,0,0" VerticalAlignment="Top" Width="497"
                     ItemsSource="{Binding}">
                <ListBox.ItemTemplate>
                    <DataTemplate>
                        <StackPanel Orientation="Horizontal">
                            <CheckBox IsChecked="{Binding IsSelected, Mode=TwoWay}"></CheckBox>
                            <TextBlock>UserName</TextBlock>
                            <TextBox Width="100" Text="{Binding UserName, Mode=TwoWay}"/>
                            <TextBlock>Password</TextBlock>
                            <TextBox Width="100" Text="{Binding Password, Mode=TwoWay}"/>
                        </StackPanel>
                    </DataTemplate>
                </ListBox.ItemTemplate>
            </ListBox>
            <Button Content="Delete" HorizontalAlignment="Left" Click="Button_Click_1" Margin="10,275,0,0" VerticalAlignment="Top" Width="101" Height="34"/>    </Grid>
    </Window>