我使用 wpf 和 MVVM。我有一个带有数据网格的页面,该页面绑定到视图模型中的数据表。在数据网格中自动生成列属性为 true。但是我需要一些组合框.所以我用数据网格的自动生成列事件来实现这一点。在代码中隐藏事件方法如下所示。
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;
}
}
现在,根据新要求,我必须使用基础设施xamDatagrid或Xamgrid来过滤值以及基础设施网格提供的其他一些功能。 但我没有找到 AutoGeneratingColumn
两个基础设施网格中的事件。如何在基础设施网格中实现此功能?
这很简单,有关详细信息,请参阅此链接
http://help.infragistics.com/NetAdvantage/WPF/2011.1/CLR4.0/?page=xamComboEditor_Using_xamComboEditor_to_Edit_a_Field_in_xamDataGrid.html
简而言之:
首先将 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>
在这种情况下,订阅 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);
}
}
有关详细信息,请参阅此内容:http://help.infragistics.com/Help/NetAdvantage/WPF/2012.2/CLR4.0/html/xamComboEditor_Setting_the_xamComboEditor_as_an_Editor_of_a_Field_Programmatically.html