如何创建简单的DataGridComboboxColumn?

时间:2016-03-17 10:36:12

标签: c# wpf xaml

如何创建简单的DataGridComboboxColumn?

我尝试创建一个包含两个字段的行:string和带有一些参数的组合框。 我可以硬编码。

但是,它不起作用。当我点击按钮"添加行":行创建,但我只能编辑文本框而没有组合框显示。

  <Grid >
                    <Grid.ColumnDefinitions>
                        <ColumnDefinition Width="100*"></ColumnDefinition>
                    </Grid.ColumnDefinitions>
                    <Grid.RowDefinitions>
                        <RowDefinition Height="5*"></RowDefinition>
                        <RowDefinition Height="95*"></RowDefinition>
                    </Grid.RowDefinitions>
                    <ToolBar  Grid.Row="0"  HorizontalAlignment="Stretch" VerticalAlignment="Stretch" Height="27" >
                        <Button x:Name="btAddAttr"  Click="btAddAttr_Click"  Height="22" Width="22"
                                    ToolTip="Add Row}" 
                                >
                            <Button.Content>
                                <Image Source="/UI.Resources;component/PNGImages/ItemAdd.png"></Image>
                            </Button.Content>
                        </Button>
                        <Button x:Name="btDelAttr"  Click="btDelAttr_Click"  Height="22" Width="22"                                                        ToolTipService.ShowOnDisabled="True"
                                    ToolTip="Remove Row"
                                    >
                            <Button.Content>
                                <Image Source="/UI.Resources;component/PNGImages/ItemDel.png">
                                </Image>
                            </Button.Content>
                        </Button>
                    </ToolBar>
                    <Grid Grid.Row="1"  x:Name="grAttrs"   HorizontalAlignment="Stretch" VerticalAlignment="Stretch"                             
                          >                            
                        <Grid.ColumnDefinitions>
                            <ColumnDefinition Width="25*"></ColumnDefinition>
                            <ColumnDefinition Width="25*"></ColumnDefinition>
                        </Grid.ColumnDefinitions>
                        <DataGrid AutoGenerateColumns="False" Grid.Row="1" Grid.Column="0" Grid.ColumnSpan="2" Name="dgAttfs"
                                  IsEnabled="True"
                                  IsReadOnly="False"
                                  CanUserAddRows="False"
                                  CanUserDeleteRows="False"
                                  Loaded="dgAttfs_Loaded"
                                  ItemsSource="{Binding ObjectsViewModel.AttributeRowColl,Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}"
                                  >
                            <DataGrid.Columns>
                                <DataGridTextColumn Binding="{Binding Name}" MinWidth="120" Width="Auto"  
                                                    Header="Name" />
                                <DataGridComboBoxColumn x:Name="typeAttributeColumn" MinWidth="220"
                                    IsReadOnly="True"
                                    SelectedItemBinding="{Binding Path=TypeAttribute}"                                        
                                    Header="List of types"                                                           
                                                        >
                                    <DataGridComboBoxColumn.ItemsSource>
                                        <col:ArrayList>
                                            <sys:String>Type1</sys:String>
                                            <sys:String>Type2</sys:String>
                                            <sys:String>Type3</sys:String>
                                            <sys:String>Type4</sys:String>
                                            <sys:String>Type5</sys:String>
                                            <sys:String>Type6</sys:String>
                                            <sys:String>Type7</sys:String>
                                            <sys:String>Type8</sys:String>
                                        </col:ArrayList>
                                    </DataGridComboBoxColumn.ItemsSource>                                                                               
                                </DataGridComboBoxColumn>                                    
                            </DataGrid.Columns>                                                                                              
                        </DataGrid>                             
                    </Grid>                        
                </Grid>

有一部分XAML代码。因此,我尝试使用XAML样式绑定一组类型,您可以在代码中看到。 并且,我在加载DataGrid时尝试绑定它。

  private void dgAttfs_Loaded(object sender, RoutedEventArgs e)
    {
        List<String> ss = new List<string>();
        ss.Add("Test");
        typeAttributeColumn.ItemsSource = ss;
    }

没有结果。

并且,当按钮点击时:

 private void btAddAttr_Click(object sender, RoutedEventArgs e)
    {
        var obj = new Row(_sm);
        _viewModel.ObjectsViewModel.AttributeRowColl.Add(obj);
        typeAttributeColumn.ItemsSource = _viewModel.ObjectsViewModel.AttributeRowColl;
        DataContext = _viewModel;
    }

Class Row:

   internal class Row:INotifyPropertyChanged
{
    private IServiceManager _serviceManager;

    private string _name;
    /// <summary>
    /// Имя Значения
    /// </summary>
    public string Name
    {
        get
        {
            return _name;
        }

        set
        {
            _name = value;
            OnPropertyChanged("Name");
        }
    }

    private ObservableCollection<String> _typesColl;
    public ObservableCollection<String> TypesColl
    {
        get
        {
            if (_typeAttribute == null)
                _typeAttribute = "Bit";

            return _typesColl;
        }

        set
        {
            _typesColl = value;
            OnPropertyChanged("TypesColl");
        }
    } 

    private String _typeAttribute;
    public String TypeAttribute
    {
        get
        {
            return _typeAttribute;
        }

        set
        {
            _typeAttribute = value;
            OnPropertyChanged("TypeAttribute");
        }
    }



    public Row(IServiceManager serviceManager)
    {
        _serviceManager = serviceManager;
        TypesColl = new ObservableCollection<string>();
        TypesColl = GetColl(serviceManager);           
    }

    #region INotifyPropertyChanged
    public event PropertyChangedEventHandler PropertyChanged;
    private void OnPropertyChanged(string propName)
    {
        PropertyChangedEventHandler eh = this.PropertyChanged;
        if (null != eh)
        {
            eh(this, new PropertyChangedEventArgs(propName));
        }
    }
    #endregion
    }

你能帮我,如何在DataGridComboBoxColumn上显示类型列表?

1 个答案:

答案 0 :(得分:2)

删除IsReadOnly =&#34; True&#34;从DataGridComboBoxColumn XAML并双击组合框列。你会得到组合框。我正在使用你的代码获得组合框。

这是因为数据网格将文本块作为单元格模板,当您尝试使用双击编辑它时,它将单元格模板更改为编辑单元格模板,数据网格编辑模板使用组合框作为其内部控件。