C# 无法编辑DataGrid中的项
我已经在主视图中添加了一个DataGrid,但是现在遇到了一个问题,我无法编辑值,也无法添加新行。虽然删除行很有效,但有人能告诉我我做错了什么吗 编辑:此项目是使用MVVM Light Toolkit创建的 MainWindow.xamlC# 无法编辑DataGrid中的项,c#,wpf,wpfdatagrid,mvvm-light,C#,Wpf,Wpfdatagrid,Mvvm Light,我已经在主视图中添加了一个DataGrid,但是现在遇到了一个问题,我无法编辑值,也无法添加新行。虽然删除行很有效,但有人能告诉我我做错了什么吗 编辑:此项目是使用MVVM Light Toolkit创建的 MainWindow.xaml <Window x:Class="PartExplorer.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="ht
<Window x:Class="PartExplorer.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:ignore="http://www.ignore.com"
mc:Ignorable="d ignore"
Height="300"
Width="300"
Title="{Binding WelcomeTitle}"
DataContext="{Binding Main, Source={StaticResource Locator}}">
<Window.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="Skins/MainSkin.xaml" />
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</Window.Resources>
<Grid x:Name="LayoutRoot">
<DataGrid ItemsSource="{Binding PriceItems, Mode=TwoWay}" IsReadOnly="False" CanUserAddRows="True" />
</Grid>
</Window>
好吧,我很蠢。
将
PriceItem
类中的setter设置为公共。复制了您的代码1:1,对我有效…@amnezjak:那么当您运行代码时,您可以编辑数据网格中的值吗?这很奇怪,因为我不能。。如果不清楚的话,我会在运行时编辑这些值。尝试在网格的items源中使用UpdateSourceTrigger=PropertyChanged,如果没有它,我永远不会离开它:D@dennissch我试着加上这个,但遗憾的是没有任何效果difference@Tom是的,我能。您所说的“无法编辑”是什么意思?就地编辑器不显示或编辑的值不传播到基础数据源?另外,DataContext可能有问题,我在code-behind.Lol中设置了我的。不,我很笨XD,我不敢相信我已经找了一个多小时了。。非常感谢你P
...
/// <summary>
/// The <see cref="PriceItems" /> property's name.
/// </summary>
public const string PriceItemsPropertyName = "PriceItems";
private ObservableCollection<PriceItem> _priceItems = new ObservableCollection<PriceItem>();
/// <summary>
/// Sets and gets the PriceBook property.
/// Changes to that property's value raise the PropertyChanged event.
/// </summary>
public ObservableCollection<PriceItem> PriceItems
{
get
{
return _priceItems;
}
set
{
if (_priceItems == value)
{
return;
}
RaisePropertyChanging(PriceItemsPropertyName);
_priceItems = value;
RaisePropertyChanged(PriceItemsPropertyName);
}
}
...
public class PriceItem
{
public PriceItem()
{
Name = "";
Price = 0;
Weight = 0;
PartType = Type.Standard;
}
public PriceItem(string name, double price, int weight, Type partType)
{
Name = name;
Price = price;
Weight = weight;
PartType = partType;
}
public Type PartType { get; private set; }
public string Name { get; private set; }
public double Price { get; private set; }
public int Weight { get; private set; }
}
public enum Type
{
Standard,
Special
}