Your Privacy Matters: We use our own and third-party cookies to improve your experience on our website. By continuing to use the website we understand that you accept their use. Cookie Policy
95
Single cell ComboBoxEditor in XamDataGrid with dynamic fields
posted

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>

Parents
  • 34510
    Verified Answer
    Offline posted

    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.

Reply Children