我目前有一个C#
WPF
.NET 4.5
个应用。我使用DataGrid
来显示项目数据库中的项目。除了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>
答案 0 :(得分:1)
为了让DataGridComboBoxColumn
正常工作,您必须使用ElementStyle
和EditingElementStyle
,如下所示:
<DataGridComboBoxColumn x:Name="cbType" Width="50" Header="Type"
DisplayMemberPath="Type" SelectedValueBinding="{Binding Type}" >
<DataGridComboBoxColumn.ElementStyle>
<Style TargetType="{x:Type ComboBox}">
<Setter Property="ItemsSource" Value="{Binding Type}"/>
</Style>
</DataGridComboBoxColumn.ElementStyle>
<DataGridComboBoxColumn.EditingElementStyle>
<Style TargetType="{x:Type ComboBox}">
<Setter Property="ItemsSource" Value="{Binding Type}"/>
</Style>
</DataGridComboBoxColumn.ElemEditingElementStyleentStyle>
</DataGridComboBoxColumn>
这将使用您的ComboBox
填充网格列中的ItemsSource
。根据您在网格中使用ComboBox
的方式,您可能不需要设置ElementStyle
。有关为何必要的信息,请参阅this answer。
答案 1 :(得分:0)
你的DataContext应该有一个ViewModel,它应该公开两个独立的属性,Inventories和TypeCollection(一个包含盎司,磅等的数组或列表),你应该将DataGridComboBoxColumn的ItemsSource绑定到TypeCollection,将SelectedValueBinding绑定到Type。
public class ViewModel{
// implement INotifyPropertyChanged for following
public IEnumerable TypeCollection{ get; private set;}
public IEnumerable Inventories {get; private set;}
}
//设置DataContext,如下所示
this.DataContext = new ViewModel(.....);
//设置Binding,如下所示
<DataGrid x:Name="dgInv" HorizontalAlignment="Center" Height="134" Margin="-10,96,10,0" VerticalAlignment="Top" Width="Auto"
IsSynchronizedWithCurrentItem="True"
ItemsSource="{Binding Inventories}"
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=DataContext.TypeCollection,
RelativeSource={RelativeSource AncestorType={x:Type DataGrid}}}}"
DisplayMemberPath="Type"
SelectedValueBinding="{Binding Path=Type, Mode=TwoWay}" >
</DataGridComboBoxColumn>
</DataGrid.Columns>
</DataGrid>
请注意,您必须使用RelativeSource来查找父DataGrid的DataContext。