以编程方式在DataGridTemplateColumn中设置ComboBox项

时间:2012-05-10 11:40:26

标签: wpf combobox datagridtemplatecolumn

我想以编程方式在DataGrid中填充ComboBox,但它不起作用。 这是XAML代码:

<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>

通过Loaded事件我想填充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;
    }

另一个版本也不起作用:

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");
    }

DataGrid中的ComboBox包含4个空项。空意味着没有“item1”......

有人能给我一个暗示我在这里做错了吗?

1 个答案:

答案 0 :(得分:2)

是不是可以命名你的组合框?然后添加项目?

<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");
    }