全选
反选
不选
///
/// 书籍
///
public class Book:INotifyPropertyChanged
{
private bool isCheck;
private int bookId;
private string bookName;
private string bookType;
private string bookCover;
private decimal bookPrice;
private int bookStock;
///
/// 是否选中
///
public bool IsCheck { get { return isCheck; } set { isCheck = value; OnPropertyChanged("IsCheck"); } }
///
/// 书籍编号
///
public int BookId { get { return bookId; } set { bookId = value;OnPropertyChanged("BookId"); } }
///
/// 书籍名
///
public string BookName { get { return bookName; } set { bookName = value; OnPropertyChanged("BookName"); } }
///
/// 书籍类型
///
public string BookType { get { return bookType; } set { bookType = value; OnPropertyChanged("BookType"); } }
///
/// 书籍封面
///
public string BookCover { get { return bookCover; } set { bookCover = value; OnPropertyChanged("BookCover"); } }
///
/// 书籍价格
///
public decimal BookPrice { get { return bookPrice; } set { bookPrice = value; OnPropertyChanged("BookPrice"); } }
///
/// 书籍库存
///
public int BookStock { get { return bookStock; } set { bookStock = value; OnPropertyChanged("BookStock"); } }
public event PropertyChangedEventHandler PropertyChanged;
protected void OnPropertyChanged(string propertyName)
{
if (PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
}
}
}
///
/// 字符串转图片路径
///
public class ImagesConverter : IValueConverter
{
private string imageDirectory = Directory.GetCurrentDirectory();
public string ImageDirectory
{
get { return imageDirectory; }
set { imageDirectory = value; }
}
public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
string imagePath = Path.Combine(ImageDirectory, "../../images/"+(string)value);
return new BitmapImage(new Uri(imagePath));
}
public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
throw new NotSupportedException("The method or operation is not implemented.");
}
}
///
/// MainWindow.xaml 的交互逻辑
///
public partial class MainWindow : Window
{
private static ObservableCollection obList = new ObservableCollection();
public MainWindow()
{
InitializeComponent();
//将数据绑定到ListView控件中
this.BooksListView.ItemsSource = InitBook();
}
///
/// 初始化书籍
///
///
private ObservableCollection InitBook()
{
for (int i = 0; i < 15; i++)
{
Book book = new Book();
book.IsCheck = false;
book.BookId = i + 1;
book.BookName = "书籍_" + (i + 1);
book.BookType = "" + (i + 1) + "";
book.BookCover= "" + (i + 1) + ".jpg";
var seed = Guid.NewGuid().GetHashCode();
Random r = new Random(seed);
book.BookPrice = r.Next(18, 70);
book.BookStock= r.Next(154, 1000);
obList.Add(book);
}
return obList;
}
///
/// 选择
///
///
///
private void Cbx_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
if ((ComboBoxItem)cbx.SelectedItem==null)
{
return;
}
string text = ((ComboBoxItem)cbx.SelectedItem).Content.ToString();
foreach (Book book in BooksListView.Items)
{
if (text == "全选")
{
book.IsCheck = true;
}
else if (text == "反选")
{
book.IsCheck = !book.IsCheck;
}
else if (text == "不选")
{
book.IsCheck = false;
}
if (book.IsCheck)
{
//避免添加重复项
if (this.selectListView.Items != null)
{
List books = this.selectListView.Items.Cast().ToList();
Book findBook = books.Where(a => a.BookId == book.BookId).SingleOrDefault();
if (findBook == null)
{
this.selectListView.Items.Add(book);
}
}
else
{
this.selectListView.Items.Add(book);
}
}
else
{
this.selectListView.Items.Remove(book);
}
}
this.cbx.SelectedIndex = -1;
}
///
/// 单击Chebox控件
///
///
///
private void Check_1_Click(object sender, RoutedEventArgs e)
{
CheckBox checkBox = (CheckBox)sender;
int id = Convert.ToInt32(checkBox.ToolTip);
if (checkBox.IsChecked == true)
{
if (this.selectListView.Items != null)
{
List books = this.selectListView.Items.Cast().ToList();
Book findBook = books.Where(a => a.BookId == id).SingleOrDefault();
if (findBook == null)
{
Book book = obList.Where(a => a.BookId == id).FirstOrDefault();
this.selectListView.Items.Add(book);
}
}
else
{
Book book = obList.Where(a => a.BookId == id).FirstOrDefault();
this.selectListView.Items.Add(book);
}
}
else
{
Book book = obList.Where(a => a.BookId == id).FirstOrDefault();
this.selectListView.Items.Remove(book);
}
}
}
添加数据前需要考虑下侧表格是否存在。