我将尝试以编程方式向DataGrid添加项目,并允许用户编辑数据。但是,当尝试编辑数据时,我不断收到“此视图不允许EditItem”错误提示。我尝试将我添加的类变为ObservableCollection,但似乎并没有改变错误。以下是我的代码片段:
XAML:
XAML:
<DataGrid x:Name="DataGridExample" HorizontalAlignment="Left" Margin="35,40,0,0" VerticalAlignment="Top" Height="220" Width="525" AutoGenerateColumns="False">
<DataGrid.Columns>
<DataGridTextColumn Binding="{Binding Path=Name}" ClipboardContentBinding="{x:Null}" Header="Filename"/>
<DataGridTextColumn Binding="{Binding Path=Prefix}" ClipboardContentBinding="{x:Null}" Header="Prefix"/>
<DataGridCheckBoxColumn Binding="{Binding Path=Sign}" ClipboardContentBinding="{x:Null}" Header="Sign"/>
<DataGridCheckBoxColumn Binding="{Binding Path=Bin}" ClipboardContentBinding="{x:Null}" Header="Bin"/>
<DataGridTextColumn Binding="{Binding Path=FolderPath}" ClipboardContentBinding="{x:Null}" Header="Folderpath"/>
</DataGrid.Columns>
</DataGrid>
MainWindowClass 添加该项:
Example newExample = new Example() { FolderPath = folderpath, Name = foldername, Prefix = foldername, Bin = false, Sign = false };
DataGridExample.Items.Add(newExample);
示例类:
public class Example : ObservableCollection<Example>
{
public string FolderPath { get; set; }
public string Name { get; set; }
public string Prefix { get; set; }
public bool Sign { get; set; }
public bool Bin { get; set; }
public override string ToString()
{
return Name;
}
}