c# – 此视图不允许使用EditItem
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了c# – 此视图不允许使用EditItem,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含2801字,纯文字阅读大概需要5分钟。
内容图文
![c# – 此视图不允许使用EditItem](/upload/InfoBanner/zyjiaocheng/786/1fcd89bb36c549238ee94061ac606fcc.jpg)
我正在尝试以编程方式将项目添加到DataGrid并让用户编辑数据.但是,在尝试编辑数据时,我不断收到“此视图不允许EditItem”错误.我尝试创建类我正在添加一个ObservableCollection但它似乎没有改变错误.这些是我的代码片段:
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;
}
}
解决方法:
xaml:
<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 :(在您的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 ).
}
内容总结
以上是互联网集市为您收集整理的c# – 此视图不允许使用EditItem全部内容,希望文章能够帮你解决c# – 此视图不允许使用EditItem所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。