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 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>
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:
Dim dataRecord As DataRecord = DirectCast(e.Record, DataRecord)dataRecord.Cells["FieldName"].EditAsType = GetType(RoundingIncrement)dataRecord.Cells["FieldName"].EditorStyle = CType(Me.Resources("RoundingStyle"), Style)
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.
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
Hi Marcus,
I'm glad to see that you got it working.