If I use an Observable collection for a property I get a collection editor in my RadPropertyGrid but all the buttons are disabled. So I tried Implementing IEditableCollectionView. It does not recognize the type and does nothing.
Please review the class definitions below or send me an example project where RadPropertyGrid will open and edit a collection of objects.
Here is the custom type where Answer is a type I cannot change but has a default constructor.
public class AnswersCollectionView<T> : IEditableCollectionView where T : Answer
{
private ObservableCollection<Answer> _answers;
public ObservableCollection<Answer> _Answers
{
get
{
if (_answers == null)
{
_answers = new ObservableCollection<Answer>();
return _answers;
}
else
{
return _answers;
}
}
set
{
_answers.Clear();
foreach (var item in value)
{
_answers.Add(item);
}
}
}
private Answer _newanswer;
public Answer NewAnswer
{
get { return _newanswer; }
private set { _newanswer = value; }
}
private Answer _editanswer;
public Answer EditAnswer
{
get { return _editanswer; }
private set { _editanswer = value; }
}
private int CurrentIndex { get; set; }
public AnswersCollectionView() { }
public object AddNew()
{
NewAnswer = new Answer();
return NewAnswer;
}
public bool CanAddNew
{
get { return true; }
}
public bool CanCancelEdit
{
get { return false; }
}
public bool CanRemove
{
get { return true; }
}
public void CancelEdit()
{
EditAnswer = null;
}
public void CancelNew()
{
NewAnswer = null;
}
public void CommitEdit()
{
_Answers[CurrentIndex] = EditAnswer;
}
public void CommitNew()
{
_Answers.Add(NewAnswer);
}
private object _currentAddItem;
public object CurrentAddItem
{
get { return _currentAddItem; }
private set { _currentAddItem = value; }
}
private object _currentEditItem;
public object CurrentEditItem
{
get { return _currentEditItem; }
private set { _currentEditItem = value; }
}
public void EditItem(object item)
{
IsEditingItem = true;
EditAnswer = (Answer)item;
CurrentIndex = _Answers.IndexOf(EditAnswer);
}
private bool _isAddingNew;
public bool IsAddingNew
{
get { return _isAddingNew; }
private set { _isAddingNew = value; }
}
private bool _isEditingItem;
public bool IsEditingItem
{
get { return _isEditingItem; }
private set { _isEditingItem = value; }
}
private NewItemPlaceholderPosition _newItemPlaceholderPosition;
public NewItemPlaceholderPosition NewItemPlaceholderPosition
{
get
{
return _newItemPlaceholderPosition;
}
set
{
_newItemPlaceholderPosition = System.ComponentModel.NewItemPlaceholderPosition.AtEnd;
}
}
public void Remove(object item)
{
if (item is Answer)
_Answers.Remove((Answer)item);
}
public void RemoveAt(int index)
{
_Answers.RemoveAt(index);
}
}
Here is the property on the class using the RADPropertyGrid:
public static readonly DependencyProperty AnswersProperty =
DependencyProperty.Register(
"Answers",
typeof(AnswersCollectionView<Answer>),
typeof(DesignerQuestionRadioButton));
[UserEditable]
[Category(DesignerCategoryConstants.Design)]
[Description("The collection of radio button answers.")]
public AnswersCollectionView<Answer> Answers
{
get { return (AnswersCollectionView<Answer>)GetValue(AnswersProperty); }
set { SetValue(AnswersProperty, value); }
}
Please review the class definitions below or send me an example project where RadPropertyGrid will open and edit a collection of objects.
Here is the custom type where Answer is a type I cannot change but has a default constructor.
public class AnswersCollectionView<T> : IEditableCollectionView where T : Answer
{
private ObservableCollection<Answer> _answers;
public ObservableCollection<Answer> _Answers
{
get
{
if (_answers == null)
{
_answers = new ObservableCollection<Answer>();
return _answers;
}
else
{
return _answers;
}
}
set
{
_answers.Clear();
foreach (var item in value)
{
_answers.Add(item);
}
}
}
private Answer _newanswer;
public Answer NewAnswer
{
get { return _newanswer; }
private set { _newanswer = value; }
}
private Answer _editanswer;
public Answer EditAnswer
{
get { return _editanswer; }
private set { _editanswer = value; }
}
private int CurrentIndex { get; set; }
public AnswersCollectionView() { }
public object AddNew()
{
NewAnswer = new Answer();
return NewAnswer;
}
public bool CanAddNew
{
get { return true; }
}
public bool CanCancelEdit
{
get { return false; }
}
public bool CanRemove
{
get { return true; }
}
public void CancelEdit()
{
EditAnswer = null;
}
public void CancelNew()
{
NewAnswer = null;
}
public void CommitEdit()
{
_Answers[CurrentIndex] = EditAnswer;
}
public void CommitNew()
{
_Answers.Add(NewAnswer);
}
private object _currentAddItem;
public object CurrentAddItem
{
get { return _currentAddItem; }
private set { _currentAddItem = value; }
}
private object _currentEditItem;
public object CurrentEditItem
{
get { return _currentEditItem; }
private set { _currentEditItem = value; }
}
public void EditItem(object item)
{
IsEditingItem = true;
EditAnswer = (Answer)item;
CurrentIndex = _Answers.IndexOf(EditAnswer);
}
private bool _isAddingNew;
public bool IsAddingNew
{
get { return _isAddingNew; }
private set { _isAddingNew = value; }
}
private bool _isEditingItem;
public bool IsEditingItem
{
get { return _isEditingItem; }
private set { _isEditingItem = value; }
}
private NewItemPlaceholderPosition _newItemPlaceholderPosition;
public NewItemPlaceholderPosition NewItemPlaceholderPosition
{
get
{
return _newItemPlaceholderPosition;
}
set
{
_newItemPlaceholderPosition = System.ComponentModel.NewItemPlaceholderPosition.AtEnd;
}
}
public void Remove(object item)
{
if (item is Answer)
_Answers.Remove((Answer)item);
}
public void RemoveAt(int index)
{
_Answers.RemoveAt(index);
}
}
Here is the property on the class using the RADPropertyGrid:
public static readonly DependencyProperty AnswersProperty =
DependencyProperty.Register(
"Answers",
typeof(AnswersCollectionView<Answer>),
typeof(DesignerQuestionRadioButton));
[UserEditable]
[Category(DesignerCategoryConstants.Design)]
[Description("The collection of radio button answers.")]
public AnswersCollectionView<Answer> Answers
{
get { return (AnswersCollectionView<Answer>)GetValue(AnswersProperty); }
set { SetValue(AnswersProperty, value); }
}