manton
manton

Reputation: 561

set items of ComboBox in DataGridTemplateColumn programmatically

I would like to fill ComboBox in a DataGrid programmatically but it doesn't work. This is the XAML code:

<DataGridTemplateColumn 
                    CellStyle="{StaticResource DataGridColumnContentCenter}"
                    local:DataGridUtil.Name="ComboBoxCol01"
                    x:Name="ComboBoxCol01"
                    Header="Maschine"
                    SortMemberPath="ComboBoxCol01"
                    IsReadOnly="True">
                    <DataGridTemplateColumn.CellTemplate>
                        <DataTemplate>
                            <ComboBox DisplayMemberPath="@name"
                                      IsSynchronizedWithCurrentItem="False"
                                      SelectedIndex="{Binding ComboBoxCol01}"
                                      IsReadOnly="True"
                                      IsEditable="True"
                                      IsDropDownOpen="False"
                                      IsHitTestVisible="True"
                                      Width="104"
                                      Loaded="ComboBoxCol01_Loaded">
                            </ComboBox>
                        </DataTemplate>
                    </DataGridTemplateColumn.CellTemplate>
                </DataGridTemplateColumn>

via the Loaded event I would like to fill the ComboBox:

private void ComboBoxCol01_Loaded(object sender, RoutedEventArgs e)
    {
        List<string> myList = new List<string>();
        myList.Add("item1");
        myList.Add("item2");
        myList.Add("item3");
        myList.Add("item4");

        ComboBox curComboBox = sender as ComboBox;
        curComboBox.ItemsSource = myList;
    }

The other version is not working also:

private void ComboBoxCol01_Loaded(object sender, RoutedEventArgs e)
    {
        ComboBox curComboBox = sender as ComboBox;
        curComboBox.Items.Add("item1");
        curComboBox.Items.Add("item2");
        curComboBox.Items.Add("item3");
        curComboBox.Items.Add("item4");
    }

ComboBoxes in the DataGrid contain 4 empty items. Empty means without "item1" ...

What am I doing wrong here?

Upvotes: 1

Views: 1081

Answers (1)

Drahadaxa
Drahadaxa

Reputation: 43

Isn't it possible to name your combobox? And then add items?

<DataGridTemplateColumn 
                    CellStyle="{StaticResource DataGridColumnContentCenter}"
                    local:DataGridUtil.Name="ComboBoxCol01"
                    x:Name="ComboBoxCol01"
                    Header="Maschine"
                    SortMemberPath="ComboBoxCol01"
                    IsReadOnly="True">
                    <DataGridTemplateColumn.CellTemplate>
                        <DataTemplate>
                            <ComboBox **x:Name="cboCol1"**
                                      DisplayMemberPath="@name"
                                      IsSynchronizedWithCurrentItem="False"
                                      SelectedIndex="{Binding ComboBoxCol01}"
                                      IsReadOnly="True"
                                      IsEditable="True"
                                      IsDropDownOpen="False"
                                      IsHitTestVisible="True"
                                      Width="104"
                                      Loaded="ComboBoxCol01_Loaded">
                            </ComboBox>
                        </DataTemplate>
                    </DataGridTemplateColumn.CellTemplate>
                </DataGridTemplateColumn>

private void ComboBoxCol01_Loaded(object sender, RoutedEventArgs e)
    {
        cboCol1.Items.Add("item1");
        cboCol1.Items.Add("item2");
        cboCol1.Items.Add("item3");
        cboCol1.Items.Add("item4");
    }

Upvotes: 2

Related Questions