Single cell ComboBoxEditor in XamDataGrid with dynamic fields

Answered (Verified) This post has 1 verified answer | 4 Replies | 2 Followers Thread's RSS feed.

MarcWeaver
Points 95
Replied On: Thu, Apr 12 2012 2:17 PM Reply

Hello-

I have an interesting scenario which I would appreciate some guidance on.  I have a xamdatagrid bound to a datatable with dynamically created fields (so no XAML code will work that I am aware of.)  I would like to have a single cell of each field use a comboboxeditor to select from a range of values.  I have it partially working.  If you look at the picture attached the fields that have numeric names are the dynamically generated ones.  I have only been able to set the whole field to the combobox editor so far, as seen in the picture.  The only one that should be a combobox is in the rounding record, the others should be decimal numbers, which work correctly otherwise.  The rounding record contains the correct values, so I am that far already, but the combobox is displayed at zero width, so that is something that needs fixed as well.

Below the picture is my VB code in the InitializeRecord event.  There are a few commented-out lines where I was trying to access the cellvaluepresenter, but I don't think it exists yet in the InitializeRecord event, so that didn't work.  I also attached the XAML for the combobox style I am trying to set it to.  The datatable contains a decimal number, which corresponds to a string name (example: .10 = "1/10 Hour")

 

 

 Private Sub grdOperations_InitializeRecord(sender As Object, e As Infragistics.Windows.DataPresenter.Events.InitializeRecordEventArgs) Handles grdOperations.InitializeRecord

        If DirectCast(DirectCast(e.Record, DataRecord).DataItem, DataRowView).Item("Name") = "Rounding" Then

            For Each f As Field In e.Record.FieldLayout.Fields

                Dim i As Integer = Nothing

                If Integer.TryParse(f.Name, i) = True Then

 

                    'Dim cvp As CellValuePresenter = CellValuePresenter.FromRecordAndField(DirectCast(e.Record, DataRecord), f)

                    'If cvp Is Nothing Then Exit Sub

                    'cvp.Style = CType(Me.Resources("RoundingStyle"), Style)

                    f.Settings.EditAsType = GetType(RoundingIncrement)

                    f.Settings.EditorStyle = CType(Me.Resources("RoundingStyle"), Style)

                End If

            Next

        End If

    End Sub

 

 

<Style x:Key="RoundingStyle" TargetType="{x:Type igEditors:XamComboEditor}">

            <Style.Setters>

                <Setter Property="ItemsSource" Value="{Binding Source={StaticResource RoundingIncrementsSource}}"/>

                <Setter Property="ValuePath" Value="Increment"/>

                <Setter Property="DisplayMemberPath" Value="Name"/>

            </Style.Setters>

        </Style>

  • Post Points: 20

Verified Answer

[Infragistics] Rob
Points 17,025
Infragistics Employee
Answered (Verified) Replied On: Fri, Apr 13 2012 12:32 PM Reply
Verified by MarcWeaver

Hi Marc,

The reason the RoundingStyle is being applied across all records is because you are appling the style to the FieldLayout.Fields object which is shared by every record.  You need to apply this style directly to the cells themselves located in the record.  You can access the cells by casting the Record to a DataRecord and using the Cells collection.  Each cell in the collection has an EditAsType and EditorStyle property where you can apply your style.  The code would look something like this:

If DirectCast(DirectCast(e.Record, DataRecord).DataItem, DataRowView).Item("Name") = "Rounding" Then

Dim dataRecord As DataRecord = DirectCast(e.Record, DataRecord)
dataRecord.Cells["FieldName"].EditAsType = GetType(RoundingIncrement)
dataRecord.Cells["FieldName"].EditorStyle = CType(Me.Resources("RoundingStyle"), Style)

End If

As for the combo box width, I believe that it should be stretching to fill the cell space automatically so I'm going to need to test this.  I'll update you on Monday once I've tested this.

Sincerely,
Rob
Developer Support Engineer
Infragistics, Inc.
www.infragistics.com/support

  • Post Points: 40

All Replies

[Infragistics] Rob
Points 17,025
Infragistics Employee
Answered (Verified) Replied On: Fri, Apr 13 2012 12:32 PM Reply
Verified by MarcWeaver

Hi Marc,

The reason the RoundingStyle is being applied across all records is because you are appling the style to the FieldLayout.Fields object which is shared by every record.  You need to apply this style directly to the cells themselves located in the record.  You can access the cells by casting the Record to a DataRecord and using the Cells collection.  Each cell in the collection has an EditAsType and EditorStyle property where you can apply your style.  The code would look something like this:

If DirectCast(DirectCast(e.Record, DataRecord).DataItem, DataRowView).Item("Name") = "Rounding" Then

Dim dataRecord As DataRecord = DirectCast(e.Record, DataRecord)
dataRecord.Cells["FieldName"].EditAsType = GetType(RoundingIncrement)
dataRecord.Cells["FieldName"].EditorStyle = CType(Me.Resources("RoundingStyle"), Style)

End If

As for the combo box width, I believe that it should be stretching to fill the cell space automatically so I'm going to need to test this.  I'll update you on Monday once I've tested this.

Sincerely,
Rob
Developer Support Engineer
Infragistics, Inc.
www.infragistics.com/support

  • Post Points: 40
[Infragistics] Rob
Points 17,025
Infragistics Employee
Replied On: Mon, Apr 16 2012 2:45 PM Reply

Hi Marc,

It looks like that Combobox behavior with the width occurs when its HorizontalAlignment is set to center.  To fix this, in the style you can add a Setter for that property and set it to Stretch.

<Setter Property="HorizontalAlignment" Value="Stretch"/>

Let me know if you have any questions on this.

Sincerely,
Rob
Developer Support Engineer
Infragistics, Inc.
www.infragistics.com/support

  • Post Points: 5
MarcWeaver
Points 95
Replied On: Tue, Apr 17 2012 2:45 PM Reply

This was the problem.  Thank you for your guidance.  As far as the combobox not filling the cell, it turns out there was a data binding error on my part, so the cell was not displaying anything, hence the small combobox.  I did not need to change the default alignments to get the combobox to display properly.

Thanks 

Marcus

  • Post Points: 20
[Infragistics] Rob
Points 17,025
Infragistics Employee
Replied On: Thu, Apr 19 2012 3:15 PM Reply

Hi Marcus,

I'm glad to see that you got it working.

Sincerely,
Rob
Developer Support Engineer
Infragistics, Inc.
www.infragistics.com/support

  • Post Points: 5
Page 1 of 1 (5 items) | RSS