我目前有一个C#
WPF
.NET 4.5
应用程序。我正在使用 aDataGrid
来显示项目数据库中的项目。我能够填充DataGrid
除DataGridComboBoxColumn
. 首先,我只想让它显示数据。将来,我希望组合框在第一行显示数据库中的当前值,但让 ComboBox 包含设置值,以限制用户可以输入的内容。所以它就像列表 item1 = 当前值,列表 item2 = 磅,列表 item3 = 盎司等。该表有 3 列我正在使用,InventoryName、Qty 和 Type。类型应该是 ComboBox
这是我的代码
private AuroraDataEntities auroraDataEntities;
public ViewCup()
{
InitializeComponent();
}
private void Window_Loaded(object sender, RoutedEventArgs e)
{
LoadData();
}
private void LoadData()
{
auroraDataEntities = new AuroraDataEntities();
dgInv.DataContext = auroraDataEntities.Inventories;
}
void EditComplete(object sender, SelectedCellsChangedEventArgs e)
{
auroraDataEntities.SaveChanges();
}
private void Window_Unloaded(object sender, RoutedEventArgs e)
{
auroraDataEntities.SaveChanges();
}
}
这是我的 XAML
<DataGrid x:Name="dgInv" HorizontalAlignment="Center" Height="134" Margin="-10,96,10,0" VerticalAlignment="Top" Width="Auto"
IsSynchronizedWithCurrentItem="True"
ItemsSource="{Binding}"
AutoGenerateColumns="False"
GridLinesVisibility="None"
SelectedCellsChanged="EditComplete">
<DataGrid.Columns>
<DataGridTextColumn Width="200" Header="InventoryName" Binding="{Binding InventoryName}" />
<DataGridTextColumn Width="50" Header="Qty" Binding="{Binding Qty}" />
<DataGridComboBoxColumn x:Name="cbType"
Width="50"
Header="Type"
ItemsSource="{Binding Path=Type, Mode=TwoWay}"
DisplayMemberPath="Type"
SelectedValueBinding="{Binding Type}" >
</DataGridComboBoxColumn>
</DataGrid.Columns>
</DataGrid>