我正在努力将复选框绑定到某些列表框值,特别是我不知道我的绑定有什么问题。我尝试了所有不同的方法,从只读属性到委托命令,甚至是单独的列表框的类。列表框包含通过使用复选框检查它们而保存的值。现在,在我的上一个版本中,我想到了两个集合,一个用于所有已教授的课程,另一个用于所选课程一位讲师教。这是我的两个系列:
这是在RegisterTeacherViewModel.cs:
中private ObservableCollection<Cours> _courses;
public ObservableCollection<Cours> Courses
{
get => _courses;
set
{
_courses = value;
NotifyOnPropertyChange(nameof(Courses));//this is the collection that holds all the courses.
}
}
private ObservableCollection<Cours> selectedCourses;
public ObservableCollection<Cours> SelectedCourses
{
get { return selectedCourses; }
set
{
selectedCourses = value;
NotifyOnPropertyChange("SelectedCourse");
}
}//this is the collection for the selected courses.
现在我有一个方法,我必须存储选定的课程以及讲师在XAML表单中插入的其他值。这是存储值的方法:
public void SaveTeacher(object param)
{
using (DatabaseStudentsEntitiesLastStand db = new
DatabaseStudentsEntitiesLastStand())
{
Cours c = new Cours();
RegisterTeacher t = new RegisterTeacher();
if(c.IsChecked==true)//here I am checking if the value "IsChecked" is true and if it is, then the course name will be stored in the DB
{
t.CourseName=courseName;
}
t.SNTeacher = SNTeacher;
t.UserName = _UserName;
t.pwd = pwd;
t.fullName = fullName;
t.education = education;
db.RegisterTeachers.Attach(t);
try
{
db.SaveChanges();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
}
现在,我还有一个“保存”按钮的委托命令:
private DelegateCommand saveCommand;
public DelegateCommand SaveCommand
{
get { return saveCommand; }
set
{
if (saveCommand != value)
{
saveCommand = value;
NotifyOnPropertyChange("SaveCommand");
}
}
}
一个名为CheckCommand的复选框的命令:
public bool IsChecked
{
get { return isChecked; }
set
{
if(isChecked!=value)
{
isChecked = value;
NotifyOnPropertyChange("IsChecked");
}
}
}
private DelegateCommand checkCommand;
public DelegateCommand CheckCommand
{
get
{
return checkCommand;
}
set
{
checkCommand = value;
NotifyOnPropertyChange("CheckCommand");
}
}
CheckCommand和IsChecked属性在模型类“Cours”中,因为我有一个绑定表达式错误,表示我必须将它们放在那里,否则绑定将不正确.check命令采用方法“CheckState” VM中遇到的问题:
public void CheckState(object param)
{
foreach(Cours item in Courses)
{
if(item.IsChecked==true)
{
SelectedCourses.Add(item);
}
}
}
所有委托命令都传递给VM中的构造函数,如下所示:
public RegisterTeacherViewModel
{
mergeCommand = new DelegateCommand(CreateCrazy);
saveCommand = new DelegateCommand(SaveTeacher);
c.CheckCommand = new DelegateCommand(CheckState);
}//as you can notice,this command has a reference to the model
类“Cours”实际实现的地方。
最后,但并非最不重要的是我有绑定的视图类:
<Window x:Class="LastAndFinalVersion.View.Register"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:LastAndFinalVersion.ViewModel"
xmlns:mm="clr-namespace:LastAndFinalVersion.Helpers"
xmlns:ms="clr-namespace:LastAndFinalVersion.Model"
mc:Ignorable="d">
<Window.DataContext>
<local:RegisterTeacherViewModel />
</Window.DataContext>
<Grid Margin="20,20,3.6,28.4">
<Grid.RowDefinitions>
<RowDefinition Height="*"/>
<RowDefinition Height="2*"/>
<RowDefinition Height="2*"/>
</Grid.RowDefinitions>
<ListBox x:Name="coursesList" SelectedValue="SelectedCourses" HorizontalAlignment="Left" SelectedItem="{Binding SelectedCourse}" Margin="418,13.2,0,0" Grid.Row="1" VerticalAlignment="Top" Width="225" Grid.RowSpan="2" ItemsSource="{Binding Courses,Mode=TwoWay}" >
<ListBox.ItemTemplate>
<DataTemplate>
<CheckBox IsChecked="{Binding IsChecked,UpdateSourceTrigger=PropertyChanged,Mode=TwoWay}" Checked="CheckBox_Checked" Command="{Binding CheckCommand,Mode=TwoWay}" Content="{Binding Path=courseName}"></CheckBox>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
<Button Content="Submit" Command="{Binding SaveCommand}" HorizontalAlignment="Left" Margin="517,98.4,0,0" Grid.Row="2" VerticalAlignment="Top" Width="110" Height="40"/>
<Label Content="Register"
</Grid>
</Window>
作为其他程序员,任何人都可以告诉我问题出在哪里,因为我没有看到它,但仍然没有按计划运行。任何建议或示例都会受到赞赏。我忘了提到我是首先使用MVVM和DB。
这就是我的xaml.cs与复选框和列表框相关的一切:
private void CheckBoxCourses_Unchecked(object sender, RoutedEventArgs e)
{
var checkBox = sender as CheckBox;
if (checkBox != null)
{
checkBox.IsChecked = true;
}
}
private void ListBox_SelectionChanged(object sender,
SelectionChangedEventArgs e)
{
((RegisterTeacherViewModel)DataContext).SelectedCourses.Clear();
foreach (Cours item in coursesList.SelectedItems)
{
((RegisterTeacherViewModel)DataContext).SelectedCourses.Add(item);
}
}
private void CheckBox_Checked(object sender, RoutedEventArgs e)
{
((CheckBox)sender).GetBindingExpression(CheckBox.IsCheckedProperty).UpdateTarget();
}
这是“Cours”课程:
using LastAndFinalVersion.Commands;
using LastAndFinalVersion.ViewModel;
using System;
using System.Collections.ObjectModel;
using System.ComponentModel;
public partial class Cours:INotifyPropertyChanged
{
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2214:DoNotCallOverridableMethodsInConstructors")]
public Cours()
{
this.RegisterTeachers = new ObservableCollection<RegisterTeacher>();
// RegisterTeacherViewModel rt = new RegisterTeacherViewModel();
// IsChecked = true;
}
public string courseName { get; set; }
public int SNTeacher { get; set; }
public string education { get; set; }
public string ClassID { get; set; }
public int courseID { get; set; }
private bool isChecked;
public event PropertyChangedEventHandler PropertyChanged;
public bool IsChecked
{
get { return isChecked; }
set
{
if(isChecked!=value)
{
isChecked = value;
NotifyOnPropertyChange("IsChecked");
}
}
}
private DelegateCommand checkCommand;
public DelegateCommand CheckCommand
{
get
{
return checkCommand;
}
set
{
checkCommand = value;
NotifyOnPropertyChange("CheckCommand");
}
}
public void SelectedItems()
{
courseName.Equals(IsChecked);
}
protected void NotifyOnPropertyChange(String propertyName)
{
if (PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
}
}
public virtual RegisterTeacher RegisterTeacher { get; set; }
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2227:CollectionPropertiesShouldBeReadOnly")]
public virtual ObservableCollection<RegisterTeacher> RegisterTeachers { get; set; }
}