Newbie question. Customizing property view

Grids for WPF Forum

Posted 8 years ago by joe sweeney - Technology Officer, TyTags Australia
Version: 10.1.0523
Avatar
Hi, I've been reading through the PropGrid manual and demos, but am stuck with what is almost certainly a total newbie issue.

I am trying to customize the categories, properties displayed and property editors in XAML, with a PropertyGrid whose SelectedObjects is set to an ArrayList of objects (all of type Person).

The following XAML is used to define the grid in the main window:
<propgrid:PropertyGrid Grid.Column="1" Name="PropertyGrid1"
VerticalAlignment="Stretch" HorizontalAlignment="Stretch" >

<propgrid:PropertyGrid.Items>
<propgrid:PropertyGridPropertyItem DisplayName="First Name"
ValueName="FirstName" Description="First name goes here." AllowDrop="True" />
<propgrid:PropertyGridPropertyItem DisplayName="Last Name" ValueName="LasttName"
Description="Family name goes here." />
</propgrid:PropertyGrid.Items>
</propgrid:PropertyGrid>

In the following code is used in the main windows code behind.


   ' Load in some sample data to the bridge, with just the first and last items being person.selected =true.
    ' assign the propertygrid1's selectedobjects to mybridge (which calls the bridge class' customer ienumerator
    Private Sub Window_Loaded(ByVal sender As System.Object, ByVal e As System.Windows.RoutedEventArgs) Handles MyBase.Loaded

       

        myBridge.ActiveList.Add(New Person("Magnus", "Devine", "1234567", Now(), "Initial Data", True))
        myBridge.ActiveList.Add(New Person("Joe", "Sweeney", "0249705842", Now(), "First example", False))
        myBridge.ActiveList.Add(New Person("Frank", "Ferlon", "0345454322", Now(), "Second example", False))
        myBridge.ActiveList.Add(New Person("Roy", "Turnbel", ")834324224", Now(), "Third example", False))
        myBridge.ActiveList.Add(New Person("Cindy", "Sweeney", "0189193413", Now(), "Forth example", True))
        PropertyGrid1.SelectedObjects = myBridge

    End Sub



myBridge is a bridge class, which implements custom iEnumerator (so that the propgrid only sees the persons that have person.selected=true. It basically reveals a series of 'person' objects to the property grid.

What I want to be able to do is customize each of the property attributes that will display on the property grid, eg. instead of showing "FirstName" and "LastName", the user will see "First Name" and "Last Name," and I wish to have some custom property editors as well.

My challenge is that when I set PropertyGrid1.SelectedObjects = myBridge, I set a runtime error with an inner exception stating "Items collection must be empty before using ItemsSource"

Can someone please provide a bit of a walk through of how to solve this.

Best regards,
JS

------------------------------------------ For all your label, tag and signage needs.

Comments (1)

Posted 8 years ago by Actipro Software Support - Cleveland, OH, USA
Avatar
Hi Joe,

There are several ways to populate the PropertyGrid. You can either add items directly to the PropertyGrid.Items collection, or bind PropertyGrid.ItemsSource to a collection. This is identical to how other ItemsControls work. In addition, to these you can also set SelectedObject(s)/Properties, which will "reflect" on the given object and set ItemsSource internally. As with any ItemsControl, you can't add items to the Items collection and use the ItemsSource at the same time.

So you would need to ensure that in your XAML that you don't include items in the PropertyGrid.

There are several ways to customize the display and we have several examples in our Sample Browser. The most basic way is to decorate your properties with attributes, such as [DisplayName("First Name")]. The list of supported .NET attributes are listed in our help file in the PropertyGrid Data Layer\Supported ComponentModel Attributes topic.


Actipro Software Support

The latest build of this product (v2018.1 build 0675) was released 14 days ago, which was after the last post in this thread.

Add Comment

Please log in to a validated account to post comments.