2

我在编辑DataGrid.

当我双击一个单元格时,会引发异常 -InvalidOperationException 'EditItem' is not allowed for this view

我正在寻找在数据网格中编辑单元格的最简单方法。

XAML------------------------------------------------- ------------------XAML

<DataGrid x:Name="ZdarzeniaDataGrid" AutoGenerateColumns="False" CanUserAddRows="True" HorizontalAlignment="Left" Margin="71,398,0,0" VerticalAlignment="Top" Height="279" Width="1119">
            <DataGrid.Columns>
                <DataGridTextColumn Binding="{Binding IdZdarzenia}" Header="Id Zdarzenia" Width="100"/>
                <DataGridTextColumn Binding="{Binding Gospodarze}"  Header="Gospodarze" Width="100"/>
                <DataGridTextColumn Binding="{Binding Goście}"  Header="Goście" Width="100"/>
                <DataGridTextColumn Binding="{Binding Kurs1}" Header="Kurs 1" Width="100"/>
                <DataGridTextColumn Binding="{Binding Kurs0}" Header="Kurs 0" Width="100"/>
                <DataGridTextColumn Binding="{Binding Kurs2}" Header="Kurs 2" Width="100"/>
                <DataGridTextColumn Binding="{Binding Kurs10}"  Header="Kurs 10" Width="100"/>
                <DataGridTextColumn Binding="{Binding Kurs20}" Header="Kurs 20" Width="100"/>
                <DataGridTextColumn Binding="{Binding Kurs12}" Header="Kurs 12" Width="100"/>
                <DataGridTextColumn Binding="{Binding DataZdarzenia}" Header="Data Zdarzenia" Width="100"/>
             </DataGrid.Columns>
        </DataGrid>

C# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - C#

ZdarzeniaDataGrid.Items.Add(
    new { IdZdarzenia = napis.ItemArray.GetValue(0).ToString(),
          Gospodarze = napis2.ItemArray.GetValue(0).ToString(), 
          Goście = napis3.ItemArray.GetValue(0).ToString(), 
          Kurs1 = napis.ItemArray.GetValue(3).ToString(), 
          Kurs0 = napis.ItemArray.GetValue(4).ToString(), 
          Kurs2 = napis.ItemArray.GetValue(5).ToString(), 
          Kurs10 = napis.ItemArray.GetValue(6).ToString(), 
          Kurs20 = napis.ItemArray.GetValue(7).ToString(), 
          Kurs12 = napis.ItemArray.GetValue(8).ToString(), 
          DataZdarzenia = napis.ItemArray.GetValue(9).ToString() 
    });
4

1 回答 1

2

创建定义了所有属性的类,并将 Datagrid 的 ItemsSource 设置/绑定到 Data 类列表。

public class Data
    {
        public string    IdZdarzenia {get;set;}
         public string Gospodarze {get;set;}
        public string Goście {get;set;}
        public string Kurs1 {get;set;}
        public string Kurs0 {get;set;}
        public string Kurs2 {get;set;}
        public string Kurs10 {get;set;}
        public string Kurs20 {get;set;}
        public string Kurs12 {get;set;}
        public string DataZdarzenia { get; set; }
    }

设置项目来源

    List<Data> dataSource = new List<Data>();
    dataSource.Add(new Data()
    {
                        IdZdarzenia = "a",
                        Gospodarze = "d",
                        Goście = "f",
                        Kurs1 = "g",
                        Kurs0 = "r",
                        Kurs2 = "gs",
                        Kurs10 = "sd",
                        Kurs20 = "sds",
                        Kurs12 = "sdss",
                        DataZdarzenia = "eeee"});
    ZdarzeniaDataGrid.ItemsSource = dataSource;

这应该可以解决您的问题。

谢谢

于 2013-08-31T18:11:16.923 回答