wpf datagrid不可编辑

时间:2013-02-16 11:20:51

标签: wpf datagrid

我对WpfDataGrid有疑问。 我创建了一个控件,这个控件打开一个窗口

var wb = new DataEntry(b, Connectionstring);
wb.Show();

DataEntry窗口包含TextBoxDataGrid,显示为不可编辑。

我读了一篇建议插入的帖子:

ElementHost.EnableModelessKeyboardInterop(wb);

之前

wb.Show();

我试过了,现在TextBox效果不错,但DataGrid仍然无法修改。

感谢您的支持。

我添加了我使用的代码

DataEntry class 

private ObservableCollection<RigaBarcode> _righeBarcode = new ObservableCollection<RigaBarcode>();


//so I create the customer object

  private class RigaBarcode : INotifyPropertyChanged 
        {
            public RigaBarcode(string barcode, string coordinate)
            {
                Barcode = barcode;
                Coordinate = coordinate;

            }

            private string _barcode;
            private string _coordinate;         

.... 惠特二传手和吸气鬼

 public event PropertyChangedEventHandler PropertyChanged;

            protected virtual void OnPropertyChanged(string propertyName)
            {
                PropertyChangedEventHandler handler = PropertyChanged;
                if (handler != null) handler(this, new PropertyChangedEventArgs(propertyName));
            }

我为此集合添加了元素

_righeBarcode.Add(new RigaBarcode("1", "2");

在xaml中我插入

DataGrid ItemsSource="{Binding .}" Name="Fustelle" Margin="10,23,10,10" Grid.Row="8" Grid.ColumnSpan="2" IsReadOnly="False" AutoGenerateColumns="False">
            DataGrid.Columns>
                    DataGridTextColumn  Header="BarCode" Width="90" Binding="{Binding Barcode}"/>
                    DataGridTextColumn  Header="Posizione" Width="90" Binding="{Binding Coordinate}"/>
                /DataGrid.Columns>
            /DataGrid>

所以我希望在初始化windows时,datagrid有1行可编辑,但datagrid不会出现这一行而我无法添加其他行

1 个答案:

答案 0 :(得分:0)

来自MSDN

  

默认情况下,您可以直接在DataGrid中编辑项目。为了保证可以正确提交和取消编辑,DataGrid中的对象必须实现IEditableObject接口。或者,您可以将IsReadOnly属性设置为true以禁用DataGrid中的编辑。

您没有说明您正在使用的框架的哪个版本以及您的数据对象是否实现了IEditableObject接口。 据我所知,这仅在.Net 4.0及更高版本中得到支持。