wpf 此视图不允许 EditItem
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/20032061/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
EditItem is not allowed for this view
提问by Deniz Zoeteman
I'm trying to add items programmaticly to a DataGrid and letting the user edit the data. However, I keep getting "EditItem is not allowed for this view" errors when trying to edit data. I tried making the class I'm adding an ObservableCollection but it doesn't seem to change the errors. These are snippets of my code:
我正在尝试以编程方式将项目添加到 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 adding the item:
MainWindowClass 添加项目:
Example newExample = new Example() { FolderPath = folderpath, Name = foldername, Prefix = foldername, Bin = false, Sign = false };
DataGridExample.Items.Add(newExample);
Example class:
示例类:
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;
}
}
采纳答案by eran otzap
xaml :
xml :
<DataGrid ItemsSource="{Binding Examples}" AutoGenerateColumns="False">
<DataGrid.Columns>
<DataGridTextColumn Binding="{Binding Path=Name}" Header="Filename"/>
<DataGridTextColumn Binding="{Binding Path=Prefix}" Header="Prefix"/>
<DataGridCheckBoxColumn Binding="{Binding Path=Sign}" Header="Sign"/>
<DataGridCheckBoxColumn Binding="{Binding Path=Bin}" Header="Bin"/>
<DataGridTextColumn Binding="{Binding Path=FolderPath}" Header="Folderpath"/>
</DataGrid.Columns>
</DataGrid>
CS: (In your MainWindow.cs)
CS:(在您的 MainWindow.cs 中)
public MainWindow()
{
InitializeComponent();
this.DataContext = this; // By setting itself to be it's own DataContext
// i can easily bind to properties in my code behind (There are other ways but this is the most correct and easy one.)
}
private ObservableCollection<Example> _examples;
public ObservableCollection<Example> Examples
{
get
{
if(_examples == null)
_examples = new ObservableCollection<Example>();
return _examples;
}
}
public void OnAddingItem()
{
Example newExample = new Example() { FolderPath = folderpath, Name = foldername, Prefix = foldername, Bin = false, Sign = false };
Examples.Add(newExample); // Because Examples is an ObservableCollection it raises a
//CollectionChanged event when adding or removing items,
// the ItemsControl (DataGrid) in your case corresponds to that event and creates a new container for the item ( i.e. new DataGridRow ).
}

