0

我使用 wpf 和 MVVM。我有一个带有 datagrid 的页面,该页面绑定到 viewmodel 的数据表。在 datagrid 中自动生成列属性为 true 。但我需要一些 coulmns 成为组合框。所以我使用数据网格的 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 网格提供的一些其他功能。但我没有 AutoGeneratingColumn在两个基础网格中找到事件。如何在基础设施网格中实现此功能?

4

2 回答 2

2

很简单,看这个链接了解详情

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>
于 2012-11-30T16:24:30.203 回答
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);
        }
    }

请参阅此以获取更多信息: http ://help.infragistics.com/Help/NetAdvantage/WPF/2012.2/CLR4.0/html/xamComboEditor_Setting_the_xamComboEditor_as_an_Editor_of_a_Field_Programmatically.html

于 2012-11-30T19:10:13.023 回答