This is a migrated thread and some comments may be shown as answers.

Collection Editor Does Not Work

3 Answers 162 Views
PropertyGrid
This is a migrated thread and some comments may be shown as answers.
Eric
Top achievements
Rank 1
Eric asked on 06 Mar 2015, 08:36 PM
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); }
}

3 Answers, 1 is accepted

Sort by
0
Stefan
Telerik team
answered on 10 Mar 2015, 10:11 AM
Hi Eric,

When using a custom collection with CollectionEditor, you need to implement both ICollectionView and IEditableCollectionView Interfaces. However, defining a custom collection is not obligatory for enabling the CollectionEditor buttons.

Since you mentioned that you have a default constructor in your business object, I am not able to reproduce the issue with ObservableCollection. I am attaching a sample project with the editing functionality working, would it be possible to modify it in a way that reproduces the issue?

I am looking forward to your answer.

Best Regards,
Stefan
Telerik
 

Check out the Telerik Platform - the only platform that combines a rich set of UI tools with powerful cloud services to develop web, hybrid and native mobile apps.

 
0
Eric
Top achievements
Rank 1
answered on 10 Mar 2015, 01:55 PM
Actually your sample project does not work. The name is not bound to the left hand list when you add an item to the collection. This is the same as another example posted by another Telerik developer.

In the 2015 Demo example, the example works but the code that was shared in the code screen does not work. I copied it word for word. Same problem. New items are not bound to the Name property in the left hand list.

See the attached image.
0
Stefan
Telerik team
answered on 13 Mar 2015, 12:24 PM
Hello Eric,

You need to explicitly define which property to be visible in the CollectionEditor, as there is no way the control to have a default binding to some property of the business object. A way of achieving this is to define a style targeting CollectionEditor and predefine the CollectionEditor ItemTemplate  within it.
Please check the following code snippet:
<Style TargetType="telerik:CollectionEditor">
    <Setter Property="ItemTemplate">
        <Setter.Value>
            <DataTemplate>
                <TextBlock Text="{Binding Name}"/>
            </DataTemplate>
        </Setter.Value>
    </Setter>
</Style>


Best Regards,
Stefan
Telerik
 

Check out the Telerik Platform - the only platform that combines a rich set of UI tools with powerful cloud services to develop web, hybrid and native mobile apps.

 
Tags
PropertyGrid
Asked by
Eric
Top achievements
Rank 1
Answers by
Stefan
Telerik team
Eric
Top achievements
Rank 1
Share this question
or