![](/img/trans.png)
[英]MVVM WPF Add new item to the DataGrid from Textboxes which are bound to the nested properties of the Model class
[英]Limit the Properties Bound to DataGrid in WPF using MVVM
一切都是一个简单的问题。 我有一个带有DataGrid
的MVVM应用程序,我已使用它绑定到ViewModel
<DataGrid ItemsSource="{Binding Path=Resources}" ...></DataGrid>
Resources
定义的地方
public ObservableCollection<ResourceViewModel> Resources { get; private set; }
但是,在ResourceViewModel
类中,我不仅具有要在DataGrid
显示的属性,还包含我不希望在DataGrid
显示的其他属性。 ResourceViewmodel
类是
public class ResourceViewModel : WorkspaceViewModel, IDataErrorInfo
{
readonly Resource resource;
readonly ResourceDataRepository resourceRepository;
private bool isSelected;
public ResourceViewModel(Resource resource, ResourceDataRepository resourceRepository)
{
if (resource == null)
throw new ArgumentNullException("resource");
if (resourceRepository == null)
throw new ArgumentNullException("resourceRepository");
this.resource = resource;
this.resourceRepository = resourceRepository;
}
public string KeyIndex
{
get { return this.resource.KeyIndex; }
set
{
if (value == this.resource.KeyIndex)
return;
this.resource.KeyIndex = value;
base.OnPropertyChanged("KeyIndex");
}
}
public string FileName
{
get { return this.resource.FileName; }
set
{
if (value == this.resource.FileName)
return;
this.resource.FileName = value;
base.OnPropertyChanged("FileName");
}
}
public List<string> ResourceStringList
{
get { return this.resource.ResourceStringList; }
set
{
if (Utilities.Utilities.ScrambledEquals<string>(this.resource.ResourceStringList, value))
return;
this.resource.ResourceStringList = value;
base.OnPropertyChanged("ResourceStringList");
}
}
public bool IsSelected
{
get { return isSelected; }
set
{
if (value == isSelected)
return;
isSelected = value;
base.OnPropertyChanged("IsSelected");
}
}
}
我不希望IsSelected
出现在DataGrid
,我希望ResourceStringList
每个项都出现在Datagrid
的不同列中。 我的问题是:
1.如何防止IsSelected
在DataGrid
显示[作为Checkbox
]?
2.如何将绑定到DataGrid
以自动在单独的列中显示项目?
你尝试了什么:
我试图从ResourceViewmodel
类继承并绑定到它,但这很恶心,我想要另一个更优雅的解决方案; 请 :]。
我不知道该如何进行。 List
存储的项目数是可变的,可以在运行时设置-因此它必须是List
。
一如既往,非常感谢您的宝贵时间。
我认为选项是关闭Silvermind提到的自动生成(即将DataGrid.AutoGenerateColumns设置为false然后定义列)或实现ITypedList。 例如,您可以创建一个派生的ObservableCollection,它实现ITypedList并根据放置在要隐藏的属性上的某个属性返回属性。
public partial class MainWindow : Window
{
public MainWindow()
{
this.DataContext = new TypedListObservableCollection<Foo>();
InitializeComponent();
}
}
public class TypedListObservableCollection<T> : ObservableCollection<T>
, ITypedList
{
public TypedListObservableCollection()
{
}
PropertyDescriptorCollection ITypedList.GetItemProperties(PropertyDescriptor[] listAccessors)
{
return TypeDescriptor.GetProperties(typeof(T), new Attribute[] { BrowsableAttribute.Yes });
}
string ITypedList.GetListName(PropertyDescriptor[] listAccessors)
{
return typeof(T).Name;
}
}
public class Foo
{
public string Name
{
get;
set;
}
[Browsable(false)]
public bool IsSelected
{
get;
set;
}
}
对我来说,不自动生成列更容易。 但这是个人偏好,所以我认为不允许暴露某些属性的最简单方法是使用接口的强大功能:)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.