![](/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.