我有一个非常简单的DataGridTextColumn
,应在doubleclick事件中对其进行修改。
问题是应该添加什么来避免出现异常System.InvalidOperationException: ''EditItem' is not allowed for this view.'
<DataGrid x:Name="DG" ItemsSource="{Binding}" GridLinesVisibility="None" Grid.Column="3" Grid.Row="2">
<DataGrid.Columns>
<DataGridTextColumn Header="Name" Binding="{Binding VariantSet, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" MinWidth="60" />
</DataGrid.Columns>
</DataGrid>
简单类:
Public Class CName
Public Property Name As String = "not editable name"
End Class
仅在加载时简单地添加到datagrid
Private Sub MainWindow_Loaded(sender As Object, e As RoutedEventArgs) Handles Me.Loaded
Me.DG.Items.Add(New CName)
End Sub
通过以下模板进行声明时,没有区别,相同错误
<DataGridTemplateColumn Header="Name" IsReadOnly="False">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<TextBlock Text="{Binding Name}" />
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
<DataGridTemplateColumn.CellEditingTemplate>
<DataTemplate>
<TextBox Text="{Binding Name}" />
</DataTemplate>
</DataGridTemplateColumn.CellEditingTemplate>
</DataGridTemplateColumn>
即使将Implements ComponentModel.INotifyPropertyChanged
添加到CName,也没有区别
答案 0 :(得分:1)
您对我们的了解不足以告诉您您在做错什么。
这是一个带有datagrid的工作窗口。
代码是c#,但是如果您特别想要vb,则可以通过转换器将其运行到vb。我认为现在初学者选择vb是个坏主意。几乎没有人使用vb发布样本。
在我的主窗口中:
<configuration>
<configSections>
<section name="CustomSection" type="ConfigurationPropertyExample.CustomSection, ConfigurationPropertyExample"
allowDefinition="Everywhere" allowExeDefinition="MachineToApplication"
restartOnExternalChanges="true" />
</configSections>
<CustomSection fileName="override.txt" alias="alias.txt"
maxUsers="1000" maxIdleTime="00:05:00" />
</configuration>
该视图模型:
<Window.DataContext>
<local:MainWindowViewModel/>
</Window.DataContext>
<Grid>
<DataGrid AutoGenerateColumns="False"
ItemsSource="{Binding People}">
<DataGrid.Columns>
<DataGridTextColumn Binding="{Binding FirstName}" Header="First Name"/>
<DataGridTextColumn Binding="{Binding LastName}" Header="SurName"/>
</DataGrid.Columns>
</DataGrid>
</Grid>
人员只有名字和姓氏这两个属性:
public class MainWindowViewModel : BaseViewModel
{
private ObservableCollection<Person> people = new ObservableCollection<Person>();
public ObservableCollection<Person> People
{
get { return people; }
set { people = value; }
}
public MainWindowViewModel()
{
People.Add(new Person { FirstName = "Chesney", LastName = "Brown" });
People.Add(new Person { FirstName = "Gary", LastName = "Windass" });
People.Add(new Person { FirstName = "Liz", LastName = "McDonald" });
People.Add(new Person { FirstName = "Carla", LastName = "Connor" });
}
}
它继承自BaseViewmodel,后者仅实现了inotifypropertychanged。
public class Person : BaseViewModel
{
private string firstName;
public string FirstName
{
get { return firstName; }
set { firstName = value; RaisePropertyChanged(); }
}
private string lastName;
public string LastName
{
get { return lastName; }
set { lastName = value; RaisePropertyChanged(); }
}
我在这里编辑一行