Hi
I have an implementation where XamDataGrid is being used. In the XamDataGrid I have a column where the value (a number) is to be changed to a corresponding text given some states.
First of all I need to have the multibinding as twoway for it to work ? If using oneway for the multibinding then it stops working.
Secondaylly, it works initially, but when editing a row, which changes the state, sometimes the number is shown instead of the text and this happens when the ConvertBack is called. I really have difficulties to implement a convertBack due to the logic, so I would like to have this to work with oneway. Se below code, where the TypeIdToTextConverter is twoway, and doesn't work if set to oneway.
<dP:Field Name="RecordType" FixedLocation="FixedToNearEdge" Width="70"> <dP:Field.Settings>
<dP:FieldSettings.EditorStyle> <Style TargetType="{x:Type igEditors:XamTextEditor}"> <Setter Property="Value"> <Setter.Value> <MultiBinding Converter="{StaticResource TypeIdToTextConverter}" > <Binding Path="Cells[RecordType].Value" Mode="OneWay"/> <Binding Path="Cells[NonVerifiedData].Value" Mode="OneWay"/> <Binding Path="Cells[NormalOperation].Value" Mode="OneWay"/> <Binding Path="Cells[CompleteRecord].Value" Mode="OneWay"/> <Binding RelativeSource="{RelativeSource AncestorType=dP:XamDataGrid}" Path="DataContext.DowntimeRecordState" Mode="OneWay"/> </MultiBinding> </Setter.Value> </Setter> </Style> </dP:FieldSettings.EditorStyle>
How can I make this work ?
Hi Dennis,
Rather than using a MultiBinding to convert your ID to text I would recommend using the XamTextEditor's ValueToDisplayTextConverter. It uses a normal IValueConverter but the parameter for the converter is set to the XamTextEditor for you. This gives you access to the DataRecord where you can get the cell values as needed. So your new converter might look something like this:
public class TypeIdToTextConverter : IValueConverter { public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture) { ValueEditor editor = parameter as ValueEditor; DataRecord record = editor.DataContext as DataRecord; var recordType = record.Cells["RecordType"].Value; var nonVerifiedData = record.Cells["NonVerifiedData"].Value; var normalOperation = record.Cells["NormalOperation"].Value; var completeRecord = record.Cells["CompleteRecord"].Value; var dataGrid = (XamDataGrid)Infragistics.Windows.Utilities.GetAncestorFromType(editor, typeof(XamDataGrid), false); var downtimeRecordState = dataGrid.DataContext.GetType().GetProperty("DowntimeRecordState").GetValue(dataGrid.DataContext); // do something with the data switch ((int)recordType) { case 0: return "zero"; case 1: return "one"; case 2: return "two"; } return value.ToString(); } public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture) { throw new NotImplementedException(); } }
Then you can simplify your style to:
<Style TargetType="{x:Type igEditors:XamTextEditor}"> <Setter Property="ValueToDisplayTextConverter" Value="{StaticResource TypeIdToTextConverter}"/> </Style>
Let me know if you have any questions on this.
Actually it just occurred to me that you might be using the MultiBinding because you want the field to update when those cell values change so my suggestion won't work. I'd still prefer to use the ValueToDisplayTextConverter though because it avoids the ConvertBack issue of the multi value converter. Are you using your own data objects for the data source or are you using a DataTable? If you are using your own POCO objects for the data items then it should be pretty simple to cause the converter to fire after updating one of the other properties (NonVerifiedData, NormalOperation, etc) by triggering a property changed notification on the RecordType. This would cause the converter to fire again.
Hi Rob,
Thanks for the suggestion, I need to update the value if the value in the field changes, not due to changes in the other columns they are only used to figure out, what the text should be.
I'm using a DataTable for the source, what I don't understand is, that I have a multibinding for the color on the same column which works fine with oneway ?
Do you know the reason for this multibinding not to work with regards to oneway, why the converter doesn't get called if I set it to oneway.
The converter does not fire because the MultiBinding is not binding to the Value property when OneWay is used. I'm not really sure why but when I inspect the visual tree at runtime and I look at the XamTextEditor.Value property I can see that the binding is not there. It most likely has something to do with the way we set the XamTextEditor's value when it is used inside a XamDataGrid.
Still, if you don't need to update the text when other columns are changed then I definitely recommend using the ValueToDisplayTextConverter. This property was meant to be used for the exact thing you are trying to do.
That works thank you, but sometimes the dataGrid in
var dataGrid = (XamDataGrid)Infragistics.Windows.Utilities.GetAncestorFromType(editor, typeof(XamDataGrid), false);
Is null ?