如何在wpf datagrid comboBox列中插入静态值?

时间:2014-08-06 07:37:51

标签: c# wpf datagrid datagridcomboboxcolumn

大家好我正在使用wpf Datagrid,我需要有一个datagridcomboBox列,我需要在该列中有一些静态值。我尝试过以下方法,但列内的数据是没有显示。

              <DataGrid
                    Grid.Row="4"
                    Height="200"
                    Width="500"
                    Grid.ColumnSpan="2"
                    Margin="10,10,0,0"
                    HorizontalAlignment="Left"
                    AutoGenerateColumns="False"
                    ItemsSource="{Binding SSID}"
                    >

                    <DataGrid.Columns>
                        <DataGridTextColumn Width="100" Header="Network ID" Binding="{Binding _networkID}"/>
                        <DataGridTextColumn Width="100" Header="SSID" Binding="{Binding _ssid}"/>
                        <DataGridComboBoxColumn Width="100" Header="Profile/Groups" SelectedItemBinding="{Binding _apProfiles}" />
                    </DataGrid.Columns>
                </DataGrid>

public List<string> _apProfiles = new List<string>()
       {
            "21-ARC1000MAP , Indoor Radio b/g/n",
             "22-ARC2000MAP , Dual Radio a/n, b/g/n"
       };

请指导我如何解决这个问题。任何帮助都会非常明显。

2 个答案:

答案 0 :(得分:1)

您需要绑定datagridcomboboxcolumn itemssource。

<DataGridComboBoxColumn Width="100" Header="Profile/Groups" ItemsSource="{Binding   _apProfiles}" />

答案 1 :(得分:0)

如果为datagrid设置数据上下文,则datagrid中的列将在datagrid的数据上下文中查找数据。如果绑定到datagridcomboBox列的值不在(SSID)datagrid的数据上下文中,则应使用

<DataGridComboBoxColumn Width="100" Header="Profile/Groups" ItemsSource="{Binding _apProfiles,  RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type UserControl}}}" />

或者像这样

<DataGridComboBoxColumn Width="100" Header="Profile/Groups" ItemsSource="{Binding RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type UserControl}}, Path=DataContext. _apProfiles}" />

希望这会有所帮助。

相关问题