我使用wpf& MVVM。 我有一个datagrid页面,它绑定到viewmodel的数据表。在datagrid中,auto generate column属性为true。但我需要一些coulmns才能成为组合框。所以我用 DataGerid的AutoGeneratingColumn事件实现了这一点。在事件方法背后的代码看起来像这样。
private void DataGrid_AutoGeneratingColumn(object sender, DataGridAutoGeneratingColumnEventArgs e)
{
if (e.PropertyName == "Gender")
{
var cb = new DataGridComboBoxColumn();
cb.ItemsSource = (DataContext as EmpDetailsWindowViewModel).GenderDataTable.DefaultView;
cb.DisplayMemberPath = "Name";
cb.SelectedValuePath = "Code";
cb.SelectedValueBinding = new Binding("Gender");
e.Column = cb;
}
}
现在根据新的要求,我必须使用infragistics xamDatagrid或Xamgrid来过滤值以及infragistics grid提供的其他一些功能。但我找不到
两个infragistics网格中的AutoGeneratingColumn
事件。如何在infragistics网格中实现此功能?
答案 0 :(得分:2)
这很简单,请参阅此链接了解详情
简而言之:
首先将ComboBoxItemsProvider添加到资源页面
<Window.Resources>
<igWPF:ComboBoxItemsProvider x:Key="cmbGenderProvider">
<igWPF:ComboBoxDataItem DisplayText="Male" Value="0"/>
<igWPF:ComboBoxDataItem DisplayText="Female" Value="1"/>
</igWPF:ComboBoxItemsProvider>
</Window.Resources>
在XAML中编辑DataGrid的字段布局,并将XamComboEditor的ItemsProvider属性设置为上述资源:
<Grid>
<igDP:XamDataGrid HorizontalAlignment="Left" DataSource="{Binding DataTable}">
<igDP:XamDataGrid.FieldLayoutSettings>
<igDP:FieldLayoutSettings AutoGenerateFields="False" />
</igDP:XamDataGrid.FieldLayoutSettings>
<igDP:XamDataGrid.FieldLayouts>
<igDP:FieldLayout>
<igDP:FieldLayout.Fields>
<igDP:Field Name="Name" Label="Name"/>
<igDP:UnboundField Name="Gender" Label="Gender">
<igDP:Field.Settings>
<igDP:FieldSettings EditAsType="{x:Type sys:Int32}" EditorType="{x:Type igWPF:XamComboEditor}">
<igDP:FieldSettings.EditorStyle>
<Style TargetType="{x:Type igWPF:XamComboEditor}">
<Setter Property="ItemsProvider" Value="{StaticResource cmbGenderProvider}"/>
</Style>
</igDP:FieldSettings.EditorStyle>
</igDP:FieldSettings>
</igDP:Field.Settings>
</igDP:UnboundField>
</igDP:FieldLayout.Fields>
</igDP:FieldLayout>
</igDP:XamDataGrid.FieldLayouts>
</igDP:XamDataGrid>
</Grid>
答案 1 :(得分:1)
在这种情况下,订阅FieldLayoutInitialized事件并编写适当的代码逻辑。
我刚写了这段代码:
private void XamDataGrid_FieldLayoutInitialized_1(object sender, Infragistics.Windows.DataPresenter.Events.FieldLayoutInitializedEventArgs e)
{
var comboGenderField = e.FieldLayout.Fields["Gender"];
if (comboGenderField != null)
{
var xamComboEditorStyle = new Style(typeof(XamComboEditor));
var itemsProviderSetter = new Setter(XamComboEditor.ItemsProviderProperty,
this.FindResource("cmbGenderProvider"));
xamComboEditorStyle.Setters.Add(itemsProviderSetter);
comboGenderField.Settings.EditorStyle = xamComboEditorStyle;
comboGenderField.Settings.EditAsType = typeof (int);
}
}