我将WPF DataGrid绑定到SQL服务器表,其中一列是comboBox。我想从另一个表填充comboBox。例如:有两个表 - 工作和员工:
这是XAML:
<Window x:Class="WpfApplication1.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">
<Grid>
<DataGrid AutoGenerateColumns="False" Height="241" HorizontalAlignment="Left" Margin="70,20,0,0" Name="EmployeeDataGrid" VerticalAlignment="Top" Width="365">
<DataGrid.Columns>
<DataGridTextColumn Header="EmployeeID" />
<DataGridTextColumn Header="E_Name" />
<DataGridTemplateColumn Header="EmployeeJobID">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<ComboBox
ItemsSource="{Binding jobs_DataView,
RelativeSource={RelativeSource AncestorType={x:Type DataGrid}}}"
DisplayMemberPath="JobName" SelectedValuePath="JobID"
SelectedValue="EmployeeJobID"/>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
</DataGrid.Columns>
</DataGrid>
</Grid>
</Window>
EmployeeDataGrid.DataContext = EmployeeDataView;
我还有一个DataView
作为名为jobsDatView
的作业表,我想将其用作项目但没有项目添加到组合框中。