我是 MVVM 新手。我在我的项目中使用 wpf 和 MVVM。所以我现在正在测试一些东西,然后再深入研究我需要编写的应用程序。
我的页面(EmpDetailsWindow.xaml)是这样的
<Grid>
<DataGrid Name="dgEmployee" Grid.Row="0" AutoGenerateColumns="True" ItemsSource="{Binding EmployeeDataTable}" CanUserAddRows="True" CanUserDeleteRows="True" IsReadOnly="False" />
<Button x:Name="btnSubmit" Content="Submit" Command="{Binding SubmitCommand}" CommandParameter="sample param" HorizontalAlignment="Left" Margin="212,215,0,0" VerticalAlignment="Top" Width="75"/>
</Grid>
我的模型(EmpDetailsWindowViewModel)如下
public class EmpDetailsWindowViewModel : INotifyPropertyChanged
{
public ICommand SubmitCommand { get; set; }
public EmpDetailsWindowViewModel()
{
EmployeeDataTable = DataTableCreator.EmployeeDataTable();
GenderDataTable = DataTableCreator.GenderDataTable();
SubmitCommand = new SubmitCommand();
}
DataTable _employeeDataTable;
public DataTable EmployeeDataTable
{
get { return _employeeDataTable;}
set
{
_employeeDataTable = value;
RaisePropertyChanged("EmployeeDataTable");
}
}
DataTable _genderDataTable;
public DataTable GenderDataTable
{
get { return _genderDataTable; }
set
{
_genderDataTable = value;
RaisePropertyChanged("GenderDataTable");
}
}
public event PropertyChangedEventHandler PropertyChanged;
public void RaisePropertyChanged(string propertyName)
{
if (PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
}
}
}
datagrid 已成功绑定到数据表。现在我在数据网格中有一个“性别”列。这应该是一个组合框,并且组合框的项目源来自视图模型的 GenderDataTable 。我怎样才能做到这一点?