C# 选择Datagrid WPF时出现错误23和错误7
在WPf、C#和使用MVVM-C的情况下,我在VS 我所说的窗口充满了一些文本框和数据网格,用户可以在其中添加新行。填写文本框时,不会显示任何问题。 由于这个问题,我无法保存更改。实际上,“保存”按钮不起作用 但只要我点击空的datagrid以添加一些数据,我就会在即时窗口中收到以下错误:C# 选择Datagrid WPF时出现错误23和错误7,c#,.net,wpf,mvvm,datagrid,C#,.net,Wpf,Mvvm,Datagrid,在WPf、C#和使用MVVM-C的情况下,我在VS 我所说的窗口充满了一些文本框和数据网格,用户可以在其中添加新行。填写文本框时,不会显示任何问题。 由于这个问题,我无法保存更改。实际上,“保存”按钮不起作用 但只要我点击空的datagrid以添加一些数据,我就会在即时窗口中收到以下错误: System.Windows.Data Error: 23 : Cannot convert '{NewItemPlaceholder}' from type 'NamedObject' to type 'L
System.Windows.Data Error: 23 : Cannot convert '{NewItemPlaceholder}' from type 'NamedObject' to type 'LIMS.ViewModels.ComponentViewModel' for 'en-US' culture with default conversions; consider using Converter property of Binding. NotSupportedException:'System.NotSupportedException: TypeConverter kan niet van MS.Internal.NamedObject worden geconverteerd.
- bij System.ComponentModel.TypeConverter.GetConvertFromException(Object value)
- bij System.ComponentModel.TypeConverter.ConvertFrom(ITypeDescriptorContext context, CultureInfo culture, Object value)
- bij MS.Internal.Data.DefaultValueConverter.ConvertHelper(Object o, Type destinationType, DependencyObject targetElement, CultureInfo culture, Boolean isForward)'
System.Windows.Data Error: 7 : ConvertBack cannot convert value '{NewItemPlaceholder}' (type 'NamedObject'). BindingExpression:Path=SelectedTestConfiguration.SelectedComponent; DataItem='TestConfigurationsPageViewModel' (HashCode=64210551); target element is 'DataGrid' (Name=''); target property is 'SelectedItem' (type 'Object') NotSupportedException:'System.NotSupportedException: TypeConverter kan niet van MS.Internal.NamedObject worden geconverteerd.
- bij MS.Internal.Data.DefaultValueConverter.ConvertHelper(Object o, Type destinationType, DependencyObject targetElement, CultureInfo culture, Boolean isForward)
- bij MS.Internal.Data.ObjectTargetConverter.ConvertBack(Object o, Type type, Object parameter, CultureInfo culture)
- bij System.Windows.Data.BindingExpression.ConvertBackHelper(IValueConverter converter, Object value, Type sourceType, Object parameter, CultureInfo culture)'
程序的屏幕截图:
每一页都有一个
- 页面控制器
- 页面视图模型
- 页面资源(xaml-代码)
<DataTemplate x:Key="TestConfigurationsDataTemplate"
DataType="{x:Type testconfigurations:TestConfigurationsPageViewModel}">
<Grid Grid.Row="3"
Grid.Column="1">
<Grid.RowDefinitions>
<RowDefinition Height="Auto"/>
<RowDefinition Height="*"/>
</Grid.RowDefinitions>
<TextBlock Text="Components" Style="{StaticResource RegularTextLeft}"/>
<DataGrid Grid.Row="1"
ItemsSource="{Binding SelectedTestConfiguration.Components}"
SelectedItem="{Binding SelectedTestConfiguration.SelectedComponent}"
AutoGenerateColumns="False"
CanUserAddRows="True"
Margin="{StaticResource SmallMargin}">
<DataGrid.Columns>
<DataGridTextColumn Header="Name" Binding="{Binding Name}"/>
<DataGridTextColumn Header="Cell" Binding="{Binding Cell}"/>
<DataGridCheckBoxColumn Header="PerformCalculation" Binding="{Binding PerformCalculation}"/>
<DataGridTextColumn Header="Calculation" Binding="{Binding Calculation}"/>
<DataGridCheckBoxColumn Header="Input Result" Binding="{Binding InputResult}"/>
</DataGrid.Columns>
</DataGrid>
</Grid>
<Button Grid.Row="5"
Grid.Column="2"
Content="Save TestConfiguration"
Command="{Binding SaveTestConfigurationCommand}"/>
PageViewModel的提取
public class TestConfigurationsPageViewModel:PageViewModel
{
private Command _searchCommand;
public Command SearchCommand
{
get { return _searchCommand; }
set
{
if (_searchCommand != value)
{
_searchCommand = value;
RaisePropertyChanged(() => SearchCommand);
}
}
}
private Command _addTestConfigurationCommand;
public Command AddTestConfigurationCommand
{
get { return _addTestConfigurationCommand; }
set
{
if (_addTestConfigurationCommand != value)
{
_addTestConfigurationCommand = value;
RaisePropertyChanged(() => AddTestConfigurationCommand);
}
}
}
private Command _saveTestConfigurationCommand;
public Command SaveTestConfigurationCommand
{
get { return _saveTestConfigurationCommand; }
set
{
if (_saveTestConfigurationCommand != value)
{
_saveTestConfigurationCommand = value;
RaisePropertyChanged(() => SaveTestConfigurationCommand);
}
}
}
private ObservableItemsCollection<TestConfigurationViewModel> _testconfigurations;
public ObservableItemsCollection<TestConfigurationViewModel> Testconfigurations
{
get { return _testconfigurations; }
set
{
if (_testconfigurations != value)
{
_testconfigurations = value;
RaisePropertyChanged(() => Testconfigurations);
}
}
}
private TestConfigurationViewModel _selectedTestConfiguration;
public TestConfigurationViewModel SelectedTestConfiguration
{
get { return _selectedTestConfiguration; }
set
{
if (_selectedTestConfiguration != value)
{
RaisePropertyChanging(() => SelectedTestConfiguration);
_selectedTestConfiguration = value;
RaisePropertyChanged(() => SelectedTestConfiguration);
}
}
}
private string _searchExpression;
public string SearchExpression
{
get { return _searchExpression; }
set
{
if (_searchExpression != value)
{
_searchExpression = value;
RaisePropertyChanged(() => SearchExpression);
}
}
}
public TestConfigurationsPageViewModel()
{
Testconfigurations = new ObservableItemsCollection<TestConfigurationViewModel>();
}
}
公共类TestConfigurationsPageViewModel:PageViewModel
{
专用命令(searchCommand);;
公共命令搜索命令
{
获取{return\u searchCommand;}
设置
{
if(_searchCommand!=值)
{
_searchCommand=值;
RaisePropertyChanged(()=>SearchCommand);
}
}
}
private命令\u addTestConfigurationCommand;
公共命令AddTestConfigurationCommand
{
获取{return\u addTestConfigurationCommand;}
设置
{
if(_addTestConfigurationCommand!=值)
{
_addTestConfigurationCommand=值;
RaisePropertyChanged(()=>AddTestConfigurationCommand);
}
}
}
私有命令_saveTestConfigurationCommand;
公共命令SaveTestConfigurationCommand
{
获取{return\u saveTestConfigurationCommand;}
设置
{
if(_saveTestConfigurationCommand!=值)
{
_saveTestConfigurationCommand=值;
RaisePropertyChanged(()=>SaveTestConfigurationCommand);
}
}
}
私有ObservableItemsCollection\u测试配置;
公共ObservietItemsCollection测试配置
{
获取{return\u testconfigurations;}
设置
{
if(_testconfigurations!=值)
{
_testconfigurations=值;
RaisePropertyChanged(()=>Testconfigurations);
}
}
}
私有测试配置视图模型\u选择测试配置;
公共测试配置视图模型选择测试配置
{
获取{return\u selectedTestConfiguration;}
设置
{
如果(_selectedTestConfiguration!=值)
{
RaisePropertyChange(()=>SelectedTestConfiguration);
_selectedTestConfiguration=值;
RaisePropertyChanged(()=>SelectedTestConfiguration);
}
}
}
私有字符串searchExpression;
公共字符串搜索表达式
{
获取{return\u searchExpression;}
设置
{
if(_searchExpression!=值)
{
_搜索表达式=值;
RaisePropertyChanged(()=>SearchExpression);
}
}
}
公共测试配置SpageViewModel()
{
Testconfigurations=新的ObservableItemsCollection();
}
}
请在绑定datagrid项的地方发布XAML代码,以及要绑定到的属性。Thx为了您的回复,我添加了所需的代码。从xaml和PageViewModel属性中提取的SelectedTestConfiguration.SelectedComponent的类型是否具有默认构造函数?否则,WPF似乎无法在运行时直接强制转换该类型,我会尝试添加一个类似于此的转换器:@Deschuytterp没有列出答案,但解决方案在Lennart所指的文章中(尽管URL现在是)。莱纳特应该得到信任。