質問

I have a DataGrid with a DataGridComboBoxColumn and I want to bind it to a static source containing only options "A","B","C"

current xaml:

<DataGridComboBoxColumn x:Name="ControlOption" Header ="ControlOption" Width="100"
    SelectedValueBinding="{Binding Operation}"
    SelectedItemBinding="{Binding Choices}"
    ScrollViewer.CanContentScroll="True"
    ScrollViewer.VerticalScrollBarVisibility="Auto"
    IsReadOnly="True"
    CanUserResize="True">

    <DataGridComboBoxColumn.ElementStyle>
        <Style TargetType="ComboBox">
            <Setter Property="ItemsSource" Value="{Binding Choices}" />
        </Style>
    </DataGridComboBoxColumn.ElementStyle>
    <DataGridComboBoxColumn.EditingElementStyle>
        <Style TargetType="ComboBox">
            <Setter Property="ItemsSource" Value="{Binding Choices}" />
        </Style>
    </DataGridComboBoxColumn.EditingElementStyle>
    <DataGridComboBoxColumn.ItemsSource>
        <col:ArrayList>
            <sys:String>AVS</sys:String>
            <sys:String>DB</sys:String>
            <sys:String>AVS_DB</sys:String>
        </col:ArrayList>
    </DataGridComboBoxColumn.ItemsSource>

</DataGridComboBoxColumn>

Please let me know how the codebehind would look like: to implement the DataGridComboBox currently I am using the codebehind as:

_ds = new DataSet();
DataTable table = new DataTable();
_ds.Tables.Add(table);
DataColumn c1 = new DataColumn("IsActive", typeof(bool));
table.Columns.Add(c1);
DataColumn c2 = new DataColumn("DataGridTextBox_QCList1", typeof(string));
table.Columns.Add(c2);
DataColumn c3 = new DataColumn("DataGridTextBox_QCSummary", typeof(string));
table.Columns.Add(c3);
DataColumn c4 = new DataColumn("Choices", typeof(string));
table.Columns.Add(c4);
datagrid.ItemsSource = _ds.Tables[0].DefaultView;

What changes do I need to make to make the necessary impact in populating the datagridcombobox (the basic problem is, I am not even seeing a dropdown)

役に立ちましたか?

解決

If you use a DataTable as the ItemsSource there is no way for the DataGrid to know that only a limited amount of values is possible for a given column, hence it will only generate DataGridTextColumns for the string columns.

You can hook the event AutoGeneratingColumn, check via the event args if the column of interest is being generated, and change the Column in the event args to a DataGridComboBoxColumn with the respective binding and ItemsSource set.

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top