Moo*_*ght 2 c# wpf binding datagrid mvvm
全部,一个简单的问题。我有一个MVVM应用程序,DataGrid
已将其绑定到使用
<DataGrid ItemsSource="{Binding Path=Resources}" ...></DataGrid>
Run Code Online (Sandbox Code Playgroud)
其中Resources
通过定义
public ObservableCollection<ResourceViewModel> Resources { get; private set; }
Run Code Online (Sandbox Code Playgroud)
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");
}
}
}
Run Code Online (Sandbox Code Playgroud)
我不想IsSelected
出现在中,DataGrid
并且我希望其中的每个项目ResourceStringList
都出现在的不同列中Datagrid
。我的问题是:
1.如何防止IsSelected
[显示为Checkbox
] DataGrid
?
2.如何获得与的绑定DataGrid
以自动在单独的列中显示项目?
您尝试了什么:
我试图从ResourceViewmodel
类继承并绑定到该类,但是这很令人讨厌,我想要另一个更优雅的解决方案。请 :]。
我不知道该如何进行。存储在中的项目数List
是可变的,可以在运行时设置-因此需要为List
。
一如既往,非常感谢您的宝贵时间。
小智 5
我认为选项是按照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;
}
}
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
390 次 |
最近记录: |