一个DataGrid中的几个组合框

时间:2018-03-12 10:44:27

标签: c# wpf

我正在尝试将每行中不同值的组合框绑定到DataGrid。我的问题很可能是我不知道如何设置ItemsSourceSelectedValuePath。应该怎么做?

的.cs:

    private List<List<ComboItem>> ComboBoxItemsList = new List<List<ComboItem>>();
    ...
    foreach (AppSettingsElement list in enablers[key])
    {   
        string[] stringSeparators = new string[] { "\r\n" };
        string[] lines = list.Comment.Split(stringSeparators, StringSplitOptions.None);
        List<ComboItem0> comboItems = GetComboItemList(lines);
        // clean comment
        list.Comment = lines[1];
        //add to comboList
        appSettingsComboBoxList.Add(new AppSettingsElement {Comment=list.Comment, Name= list.Name, Value=list.Value, ComboItems = comboItems });
    }
    AppSettingsDropDowns.ItemsSource = appSettingsComboBoxList;

    ...
    public class ComboItem0
    {
        public string ID { get; set; }
        public string Text { get; set; }    
    }
    public class AppSettingsElement : ComboItem0
    {
    public string Comment { get; set; }
    public string Name { get; set; }
    public string Value { get; set; }
    public string Type { get; set; }
    public List<ComboItem0> ComboItems { get; set; }
}

的.xaml:

        <DataGrid x:Name="AppSettingsDropDowns" AutoGenerateColumns="False" Grid.ColumnSpan="6" Grid.Row="8" CanUserAddRows="False" >
            <DataGrid.Columns>
                <DataGridTextColumn Binding="{Binding Comment}" Header="Comment" Width="4*" IsReadOnly="True" />
                <DataGridTextColumn Binding="{Binding Name}" Header="Element Name" Width="*" IsReadOnly="True"/>
                <DataGridComboBoxColumn ItemsSource="{Binding ComboItems}" SelectedValueBinding="{Binding Value}" SelectedValuePath="ID" DisplayMemberPath="Text" Header="Value" Width="*" />
            </DataGrid.Columns>
        </DataGrid>

编辑:我的代码目前显示空白的组合框选择。

1 个答案:

答案 0 :(得分:0)

    <DataGrid x:Name="AppSettingsDropDowns" AutoGenerateColumns="False" Grid.ColumnSpan="6" Grid.Row="8" CanUserAddRows="False" >
        <DataGrid.Columns>
            <DataGridTextColumn Binding="{Binding Comment}" Header="Comment" Width="4*" IsReadOnly="True" />
            <DataGridTextColumn Binding="{Binding Name}" Header="Element Name" Width="*" IsReadOnly="True"/>
            <DataGridComboBoxColumn DisplayMemberPath="Text" SelectedValuePath="ID"  Header="Value" Width="*" >
                <DataGridComboBoxColumn.ElementStyle>
                    <Style TargetType="{x:Type ComboBox}">
                        <Setter Property="ItemsSource" Value="{Binding ComboItems}"/>
                        <Setter Property="SelectedValue" Value="{Binding Value}" />                                
                    </Style>
                </DataGridComboBoxColumn.ElementStyle>
                <DataGridComboBoxColumn.EditingElementStyle>
                    <Style TargetType="{x:Type ComboBox}">
                        <Setter Property="ItemsSource" Value="{Binding ComboItems}"/>
                        <Setter Property="SelectedValue" Value="{Binding Value}" />
                    </Style>
                </DataGridComboBoxColumn.EditingElementStyle>
            </DataGridComboBoxColumn>
        </DataGrid.Columns>
    </DataGrid>