Hi,
I want to write a fixed ControlTemplate for all unboundfields(So i can't write the full bindingPath in this controlTemplate ) which the cell value is a Type A object in my project. But i can't get the Type A object ,the value of ValueEditor is a string.
I'm using AlternateBinding to set the Type A object to this field on field's definition.
Binding alternateBinding = new Binding(); alternateBinding.Path = new PropertyPath(string.Format("Fields[{0}]", fieldEditor.Name));
Field field = new Field() { Name = fieldEditor.Name, Label = fieldEditor.Label, Column = startColumnIndex, BindingType = BindingType.UseAlternateBinding, AlternateBinding = alternateBinding}
My template:
<ControlTemplate x:Key="stringEditorTemplate" TargetType="{x:Type igEditors:ValueEditor}"> <Grid DataContext="{TemplateBinding Value}" Visibility="{Binding IsVisible,Converter={StaticResource BoolToVisibilityConverter}}"> <local:RMTextBlock Text="{Binding Text}" Style="{StaticResource StringEditorTemplateTextBlockStyle}"/> </Grid> </ControlTemplate>
editorStyle.Setters.Add(new Setter(ValueEditor.TemplateProperty, Application.Current.Resources["stringEditorTemplate"]));
field.Settings.EditorStyle = editorStyle;
------------------------------------------------------------------------------------------
If i'm using RelativeSource to find the value of CellValuePresenter is correct. But the output print binding errors while the data record is initializing.
<ControlTemplate x:Key="stringEditorTemplate" TargetType="{x:Type igEditors:ValueEditor}"> <Grid DataContext="{Binding Value,RelativeSource={RelativeSource AncestorType={x:Type igDP:CellValuePresenter}}}" Visibility="{Binding IsVisible,Converter={StaticResource BoolToVisibilityConverter}}"><local:RMTextBlock Text="{Binding Text}" Style="{StaticResource StringEditorTemplateTextBlockStyle}"/></Grid></ControlTemplate>
So , Neither using RelativeSource nor using TemplateBinding the Value is not the best way to bind. Can someone give me advice for this situation?
Hi Brandon,
I think I might be missing something here. Could you please create a small sample illustrating the approach with TemplateBinding so I could look further into it?
Thank you for reply. I just want to find a best solution to let the value showing is correct.
See the attachment.
BTW, the first approach is not satisfied when double click to edit it. I used it before.(Actually, this issue only appear on IG 15.1,old version doesn't have this issue.)
But the second approach is perfect. Thank you again.
Hi Konstantin,
This is very helpful for me. Thank you very much.
Thanks for the sample – it really helped. With the first approach the issue was that since there is no EditAsType property set to the field the grid was assuming it to be string and have been overriding the ValueType set to ValueEditor as part of the style. When I set EditAsType to be ObjectA the grid is throwing an exception since it is trying to create an instance ValueEditor since it was trying to infer the editor type from the EditorStyle’s TargetType. I suppose the binding errors were appairing probably because at some point the editor is not yet into the visual tree and the RelativeSource binding are not able to look up the corresponding parent.
I’ve come up with two options for you to achieve this. The first one is basically re-templating the CellValuePresenter instead of the editor itself(which at the end is provided as Content of the CellValuePresenter).
The second approach is to create new CustomValueEditor inheriting from ValueEditor and implementing its two abstract methods(CanEditType, CanRenderType) and then set the Field up so its EditAsType is set to ObjectA and EditorType is set to be that CustomValueEditor and then target it in the EditorStyle. This second approach could probably provide you some more flexibility with regard to integrating this field well with the rest of XamDataGrid features.
Check out the attached sample illustrating both of the approaches described above.
Please don’t hesitate to ask if you have further questions on the matter.