Home Forums WPF controls Xceed DataGrid for WPF How to use a ListBox in a CellContentTemplate

Viewing 8 posts - 1 through 8 (of 8 total)
  • Author
    Posts
  • SezMe
    Participant
    Post count: 4
    #45086 |

    I have a Class called Planner with the usual properties (Name, Address, etc.) and an ObserveableCollection(Of Planner). I use this collection as follows:

    cvsPlanners.Source= <the ObserveableCollection mentioned above>

    I want to use a ListBox in a gridcontrol to display the name property. This is my XAML:

    <xcdg:Column
    	FieldName="PlannerID"
    	Title="Planner">
    	<xcdg:Column.CellContentTemplate>
    		<DataTemplate>
    			<ListBox
    				ItemsSource="{Binding Source={StaticResource cvsPlanners}}"
    			DisplayMemberPath="FullName"
    				SelectedValuePath="PlannerID">
    			</ListBox>
    		</DataTemplate>
    	</xcdg:Column.CellContentTemplate>
    </xcdg:Column>

    But this is not working – a name appears in the cell but not in a ListBox and not the correct name.
    What is my error?

    Diane [Xceed]
    Moderator
    Post count: 1353

    Hi SezMe,

    Columns have both a CellContentTemplate and a CellEditor. The CellContentTemplate is only used for display, it is the template shown when the cell is not in edit mode.

    For the template used in edit mode, you must use the CellEditor instead. Also note that you must make use of the {xcdg:CellEditorBinding} for the binding to work correctly.

    For example:

       <xcdg:Column FieldName="test" ...>
          <xcdg:Column.CellEditor>
             <xcdg:CellEditor>
                <xcdg:CellEditor.EditTemplate>
                   <DataTemplate>
                      <TextBox Text="{xcdg:CellEditorBinding}" />
                   </DataTemplate>
                </xcdg:CellEditor.EditTemplate>
             </xcdg:CellEditor>
          </xcdg:Column.CellEditor>
       </xcdg:Column>
    SezMe
    Participant
    Post count: 4

    Thanks for the comment. I get the concept but my implementation still does not work. I want my cell editor to be a ListBox so here is the XAML I used:

    <xcdg:Column
    	FieldName="PlannerID"
    	Title="Planner">
    	<xcdg:Column.CellContentTemplate>
    		<DataTemplate>
    			<TextBlock Text="{Binding Path=FullName}" />
    		</DataTemplate>
    	</xcdg:Column.CellContentTemplate>
    	<xcdg:Column.CellEditor>
    		<xcdg:CellEditor>
    			<xcdg:CellEditor.EditTemplate>
    				<DataTemplate>
    					<ListBox 
    						ItemsSource="{Binding Source={StaticResource cvsPlanners}}"
    						SelectedValue="{xcdg:CellEditorBinding}" 
    						SelectedValuePath="PlannerID"
    						DisplayMemberPath="FullName">
    					</ListBox>
    				</DataTemplate>
    			</xcdg:CellEditor.EditTemplate>
    		</xcdg:CellEditor>
    	</xcdg:Column.CellEditor>
    </xcdg:Column>

    This also does not work. The cell displays FullNamne (a read-only property of the Planner class) and no ListBoxc appears when the cell is in edit mode.

    Diane [Xceed]
    Moderator
    Post count: 1353

    Hi SezMe,

    The problem is that the DataTemplate does not have access to static resources on the page, since the context of the DataTemplate is not the Xaml parser (and hence the page), but the column (and hence the DataGrid). Therefore, you need to provide a relative resource that points to the collection, normally a property on an object accessible to the DataGrid. Moreover, this type of scenario is normally handled through a ForeignKey, which the grid supports out of the box.

    You simply need to provide a ForeignKeyConfiguration on the column, and the editor will automatically display a ListBox. You can look at our TableView sample (default installation path: “C:\Xceed Samples\Xceed DataGrid for WPF v6.4\CSharp\TableView”) which demonstrates how to do this.

    It looks like :

       <Page.Resources>
          <ResourceDictionary>
    
             [...]
    
             <xcdg:ForeignKeyConfiguration x:Key="shipViaForeignKeyConfiguration"
                                           ItemsSource="{Binding Source={x:Static Application.Current},Path=Shippers}"
                                           ValuePath="ShipperID"
                                           DisplayMemberPath="CompanyName" />
          </ResourceDictionary>
       </Page.Resources>
       <xcdg:Column FieldName="ShipVia"
                    Title="Shipper"
                    ForeignKeyConfiguration="{StaticResource shipViaForeignKeyConfiguration}" />
    SezMe
    Participant
    Post count: 4

    That approach is much simpler but I cannot get my implementation of it to work. Here is how I set up the ForeignKeyConfiguration:

    <xcdg:ForeignKeyConfiguration 
    	x:Key="fkcPlanner"
    	ItemsSource="{Binding Source={StaticResource cvsPlanners}}"
    	ValuePath="PlannerID"
    	DisplayMemberPath="FullName">
    </xcdg:ForeignKeyConfiguration>

    where cvsPlanners is a CollectionViewSource the source of which is an ObserveableCollection.

    The corresponding column is:

    <xcdg:Column
    	FieldName="PlannerID"
    	Title="Planner"
    	Width="140" 
    	ForeignKeyConfiguration="{StaticResource fkcPlanner}">
    </xcdg:Column>

    When I run the app the Planner column displays the PlannerID not the FullName and the dropdown contains four rows (which is correct) but they all show HIPPO.Planner. HIPPO is just the name of the project.

    As far as I can tell, this is a mirror of your suggested approach. Can you identify my error.

    By the way, I do not have that example you mentioned. I only have the Live Explorer which does not use a foreign key.

    Diane [Xceed]
    Moderator
    Post count: 1353

    Hi SezMe,

    You mention that you do not have the sample I mentioned. Just to be sure, are you using the Xceed DataGrid for WPF? or the datagrid found in the Xceed Toolkit Plus for WPF?

    SezMe
    Participant
    Post count: 4

    The latter. The freebie one. If I can get this and a few other features working, I’ll upgrade.

    Diane [Xceed]
    Moderator
    Post count: 1353

    Hi SezMe,

    I would recommend downloading a trial of the Toolkit Plus and the DataGrid for WPF, to see which datagrid is a better fit for your needs. Each includes a 45-day trial key to test the component. Also, with the DataGrid for WPF package, you will have access to the sample project I mentioned previously.

    Xceed Toolkit Plus for WPF
    http://forums.xceed.com/xceed-toolkit-plus-for-wpf/

    Xceed DataGrid for WPF
    http://forums.xceed.com/xceed-datagrid-for-wpf/

Viewing 8 posts - 1 through 8 (of 8 total)
  • You must be logged in to reply to this topic.