C# WPF绑定列表<;字符串>;到组合框
我的问题是组合框列表为空。我浏览了很多网站(在3天内),但都没弄明白。C# WPF绑定列表<;字符串>;到组合框,c#,wpf,C#,Wpf,我的问题是组合框列表为空。我浏览了很多网站(在3天内),但都没弄明白。 我已经编写了一个程序,显示学生列表,单击我可以更改他们的属性。因此,我可以成功更改除教员(组合框)之外的所有属性。 我使用了MVVM… 我有一个班的学生(模特)。这里,通用是enum学生教员 using System; using System.Collections.Generic; using System.ComponentModel; using System.Linq; using System.Text; us
我已经编写了一个程序,显示学生列表,单击我可以更改他们的属性。因此,我可以成功更改除教员(组合框)之外的所有属性。
我使用了MVVM…
我有一个班的学生(模特)。这里,通用是enum学生教员
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace _15._01._2018.Model
{
public enum StudentFaculty { Programmer, SysAdministration, Designer};
class Student : INotifyPropertyChanged
{
private string _name;
private string _lastname;
private StudentFaculty _faculty;
private double _averageMark;
public string Name
{
get { return _name; }
set
{
if(_name == value) return;
_name = value;
OnPropertyChanged("Name");
}
}
public string Lastname
{
get { return _lastname; }
set
{
if (_lastname == value) return;
_lastname = value;
OnPropertyChanged("Lastname");
}
}
public StudentFaculty Faculty
{
get { return _faculty; }
set
{
if (_faculty == value) return;
_faculty = value;
OnPropertyChanged("Faculty");
}
}
public double AverageMark
{
get { return _averageMark; }
set
{
if (_averageMark == value) return;
_averageMark = value;
OnPropertyChanged("AverageMark");
}
}
public Student() { }
public Student(string name, string lastname, StudentFaculty faculty, double averageMark)
{
Name = name;
Lastname = lastname;
Faculty = faculty;
AverageMark = averageMark;
}
public event PropertyChangedEventHandler PropertyChanged;
public void OnPropertyChanged(string property)
{
if (PropertyChanged != null)
PropertyChanged(this, new PropertyChangedEventArgs(property));
}
}
}
类ApplicationViewModel。在这里,我从构造函数中的枚举中列出(学院),也选择了学院
using _15._01._2018.Model;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace _15._01._2018.ViewModel
{
class ApplicationViewModel : INotifyPropertyChanged
{
private Student _selectedStudent;
private string _selectedFaculty;
public ObservableCollection<Student> Students { get; set; }
public List<string> Faculties { get; set; }
public Student SelectedStudent
{
get { return _selectedStudent; }
set
{
if (_selectedStudent == value) return;
_selectedStudent = value;
OnChangedProperty("SelectedStudent");
}
}
public string SelectedFaculty
{
get { return _selectedFaculty; }
set
{
if (_selectedFaculty == value) return;
_selectedFaculty = value;
OnChangedProperty("SelectedFaculty");
}
}
public ApplicationViewModel()
{
Students = new ObservableCollection<Student>
{
new Student("Vova", "Zyabkin", StudentFaculty.Programmer, 9.4),
new Student("Vadym", "Lazariev", StudentFaculty.Programmer, 9),
new Student("SvEta", "Belyaeva", StudentFaculty.Designer, 9.8),
new Student("Vova", "Skachkov", StudentFaculty.SysAdministration, 8.7)
};
Faculties = new List<string>(Enum.GetNames(typeof(StudentFaculty)));
}
public event PropertyChangedEventHandler PropertyChanged;
public void OnChangedProperty(string property)
{
if (PropertyChanged != null)
PropertyChanged(this, new PropertyChangedEventArgs(property));
}
}
}
使用_15._01._2018.Model;
使用制度;
使用System.Collections.Generic;
使用System.Collections.ObjectModel;
使用系统组件模型;
使用System.Linq;
使用系统文本;
使用System.Threading.Tasks;
名称空间_15._01._2018.ViewModel
{
类ApplicationViewModel:INotifyPropertyChanged
{
私人学生(u selected Student);;
私有字符串_selectedFaculty;
公共可观察集合学生{get;set;}
公共列表学院{get;set;}
公共学生选择学生
{
获取{return\u selectedStudent;}
设置
{
if(_selectedStudent==value)返回;
_selectedStudent=value;
一旦更改属性(“选定学生”);
}
}
公共字符串SelectedFaculty
{
获取{return\u selectedFaculty;}
设置
{
if(_selectedFaculty==value)返回;
_selectedFaculty=值;
一旦更改属性(“选定教员”);
}
}
公共应用程序视图模型()
{
学生=新观察到的集合
{
新学生(“Vova”,“Zyabkin”,学生教员,程序员,9.4),
新生(“Vadym”,“Lazariev”,学生教员,程序员,9岁),
新生(“SvEta”,“Belyaeva”,学生系设计师,9.8),
新学生(“Vova”,“Skachkov”,StudentFaculty.SysAdministration,8.7)
};
学院=新列表(Enum.GetNames(typeof(StudentFaculty));
}
公共事件属性更改事件处理程序属性更改;
公共void OnChangedProperty(字符串属性)
{
if(PropertyChanged!=null)
PropertyChanged(此,新PropertyChangedEventArgs(property));
}
}
}
XAML
我想用字符串属性创建一个类,但它与ListBox和Student类似。首先,ComboBox的DataContext设置为SelectedStudent,而不是ApplicationViewModel(请参见父StackPanel)。其次,Faculties属性返回字符串列表,但Student类的属性为StudentFaculty(为SelectedValue绑定将不起作用) 请尝试以下操作: 模型/视图模型:
class ApplicationViewModel : INotifyPropertyChanged
{
private Student _selectedStudent;
private StudentFaculty _selectedFaculty;
public ObservableCollection<Student> Students { get; set; }
public ObservableCollection<StudentFaculty> Faculties { get; set; }
public Student SelectedStudent
{
get => _selectedStudent;
set
{
if (_selectedStudent == value) return;
_selectedStudent = value;
OnChangedProperty("SelectedStudent");
}
}
public StudentFaculty SelectedFaculty
{
get => _selectedFaculty;
set
{
if (_selectedFaculty == value) return;
_selectedFaculty = value;
OnChangedProperty("SelectedFaculty");
}
}
public ApplicationViewModel()
{
Students = new ObservableCollection<Student>
{
new Student("Vova", "Zyabkin", StudentFaculty.Programmer, 9.4),
new Student("Vadym", "Lazariev", StudentFaculty.Programmer, 9),
new Student("SvEta", "Belyaeva", StudentFaculty.Designer, 9.8),
new Student("Vova", "Skachkov", StudentFaculty.SysAdministration, 8.7)
};
Faculties = new ObservableCollection<StudentFaculty>(Enum.GetValues(typeof(StudentFaculty)).OfType<StudentFaculty>());
}
public event PropertyChangedEventHandler PropertyChanged;
public void OnChangedProperty(string property) => PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(property));
}
public enum StudentFaculty { Programmer, SysAdministration, Designer };
class Student : INotifyPropertyChanged
{
private string _name;
private string _lastname;
private StudentFaculty _faculty;
private double _averageMark;
public string Name
{
get => _name;
set
{
if (_name == value) return;
_name = value;
OnPropertyChanged("Name");
}
}
public string Lastname
{
get => _lastname;
set
{
if (_lastname == value) return;
_lastname = value;
OnPropertyChanged("Lastname");
}
}
public StudentFaculty Faculty
{
get => _faculty;
set
{
if (_faculty == value) return;
_faculty = value;
OnPropertyChanged("Faculty");
}
}
public double AverageMark
{
get => _averageMark;
set
{
if (_averageMark == value) return;
_averageMark = value;
OnPropertyChanged("AverageMark");
}
}
public Student() { }
public Student(string name, string lastname, StudentFaculty faculty, double averageMark)
{
Name = name;
Lastname = lastname;
Faculty = faculty;
AverageMark = averageMark;
}
public event PropertyChangedEventHandler PropertyChanged;
public void OnPropertyChanged(string property) => PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(property));
}
class ApplicationViewModel:INotifyPropertyChanged
{
私人学生(u selected Student);;
私立学生学院(u选择的学院),;
公共可观察集合学生{get;set;}
公共可观测集合能力{get;set;}
公共学生选择学生
{
get=>\u selectedStudent;
设置
{
if(_selectedStudent==value)返回;
_selectedStudent=value;
一旦更改属性(“选定学生”);
}
}
公共学生教员选定教员
{
get=>\u选择的教员;
设置
{
if(_selectedFaculty==value)返回;
_selectedFaculty=值;
一旦更改属性(“选定教员”);
}
}
公共应用程序视图模型()
{
学生=新观察到的集合
{
新学生(“Vova”,“Zyabkin”,学生教员,程序员,9.4),
新生(“Vadym”,“Lazariev”,学生教员,程序员,9岁),
新生(“SvEta”,“Belyaeva”,学生系设计师,9.8),
新学生(“Vova”,“Skachkov”,StudentFaculty.SysAdministration,8.7)
};
Faculties=新的ObservableCollection(Enum.GetValues(typeof(StudentFaculty)).OfType());
}
公共事件属性更改事件处理程序属性更改;
public void OnChangedProperty(string属性)=>PropertyChanged?.Invoke(这是新的PropertyChangedEventArgs(属性));
}
公共enum学生学院{程序员、系统管理、设计师};
班级学生:INotifyPropertyChanged
{
私有字符串\u名称;
私有字符串_lastname;
私立学生学院;
私人双平均柜;
公共字符串名
{
get=>\u name;
设置
{
if(_name==value)返回;
_名称=值;
不动产变更(“名称”);
}
}
公共字符串姓氏
{
get=>\u lastname;
设置
{
if(_lastname==value)返回;
_lastname=值;
OnPropertyChanged(“姓氏”);
}
}
公立学生学院
{
get=>\U教员;
设置
{
if(_=value)返回;
_能力=价值;
关于财产变更(“教员”);
}
}
公共双平均柜
{
get=>\u averageMark;
设置
{
如果(_averageMark==值)re
class ApplicationViewModel : INotifyPropertyChanged
{
private Student _selectedStudent;
private StudentFaculty _selectedFaculty;
public ObservableCollection<Student> Students { get; set; }
public ObservableCollection<StudentFaculty> Faculties { get; set; }
public Student SelectedStudent
{
get => _selectedStudent;
set
{
if (_selectedStudent == value) return;
_selectedStudent = value;
OnChangedProperty("SelectedStudent");
}
}
public StudentFaculty SelectedFaculty
{
get => _selectedFaculty;
set
{
if (_selectedFaculty == value) return;
_selectedFaculty = value;
OnChangedProperty("SelectedFaculty");
}
}
public ApplicationViewModel()
{
Students = new ObservableCollection<Student>
{
new Student("Vova", "Zyabkin", StudentFaculty.Programmer, 9.4),
new Student("Vadym", "Lazariev", StudentFaculty.Programmer, 9),
new Student("SvEta", "Belyaeva", StudentFaculty.Designer, 9.8),
new Student("Vova", "Skachkov", StudentFaculty.SysAdministration, 8.7)
};
Faculties = new ObservableCollection<StudentFaculty>(Enum.GetValues(typeof(StudentFaculty)).OfType<StudentFaculty>());
}
public event PropertyChangedEventHandler PropertyChanged;
public void OnChangedProperty(string property) => PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(property));
}
public enum StudentFaculty { Programmer, SysAdministration, Designer };
class Student : INotifyPropertyChanged
{
private string _name;
private string _lastname;
private StudentFaculty _faculty;
private double _averageMark;
public string Name
{
get => _name;
set
{
if (_name == value) return;
_name = value;
OnPropertyChanged("Name");
}
}
public string Lastname
{
get => _lastname;
set
{
if (_lastname == value) return;
_lastname = value;
OnPropertyChanged("Lastname");
}
}
public StudentFaculty Faculty
{
get => _faculty;
set
{
if (_faculty == value) return;
_faculty = value;
OnPropertyChanged("Faculty");
}
}
public double AverageMark
{
get => _averageMark;
set
{
if (_averageMark == value) return;
_averageMark = value;
OnPropertyChanged("AverageMark");
}
}
public Student() { }
public Student(string name, string lastname, StudentFaculty faculty, double averageMark)
{
Name = name;
Lastname = lastname;
Faculty = faculty;
AverageMark = averageMark;
}
public event PropertyChangedEventHandler PropertyChanged;
public void OnPropertyChanged(string property) => PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(property));
}
<Window x:Class="WpfApp4.MainWindow"
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:converters="clr-namespace:WpfApp4.Views.Converters"
xmlns:i="http://schemas.microsoft.com/expression/2010/interactivity"
xmlns:ei="http://schemas.microsoft.com/expression/2010/interactions"
xmlns:viewModels="clr-namespace:WpfApp4.ViewModels"
mc:Ignorable="d"
Title="MainWindow" Height="350" Width="525">
<Window.DataContext>
<viewModels:ApplicationViewModel />
</Window.DataContext>
<Window.Resources>
<Style TargetType="TextBlock">
<Setter Property="FontSize" Value="14" />
</Style>
<Style TargetType="TextBox">
<Setter Property="FontSize" Value="14" />
</Style>
<CollectionViewSource x:Key="Faculties" Source="{Binding Faculties}"></CollectionViewSource>
</Window.Resources>
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition></ColumnDefinition>
<ColumnDefinition></ColumnDefinition>
</Grid.ColumnDefinitions>
<ListBox ItemsSource="{Binding Students}" SelectedItem="{Binding SelectedStudent}">
<ListBox.ItemTemplate>
<DataTemplate>
<StackPanel Orientation="Vertical">
<TextBlock Text="{Binding Path=Name}"></TextBlock>
<TextBlock Text="{Binding Path=Lastname}"></TextBlock>
<!--<TextBlock Text="{Binding Path=Faculty}"></TextBlock>-->
<TextBlock Text="{Binding Path=AverageMark}"></TextBlock>
</StackPanel>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
<Grid Grid.Column="1">
<StackPanel DataContext="{Binding SelectedStudent}">
<TextBlock Text="D A T A"></TextBlock>
<TextBlock Text="Name:"></TextBlock>
<TextBox Text="{Binding Name, UpdateSourceTrigger=PropertyChanged}"></TextBox>
<TextBlock Text="Lastname:"></TextBlock>
<TextBox Text="{Binding Lastname, UpdateSourceTrigger=PropertyChanged}"></TextBox>
<TextBlock Text="Faculty:"></TextBlock>
<ComboBox ItemsSource="{Binding Source={StaticResource Faculties}}" SelectedValue="{Binding Faculty, Mode=TwoWay}">
</ComboBox>
<TextBlock Text="Average Mark:"></TextBlock>
<TextBox Text="{Binding AverageMark, UpdateSourceTrigger=PropertyChanged}"></TextBox>
</StackPanel>
</Grid>
</Grid>