We are trying to develop a grid whose AllowEdit property for field settigs depends on some runtime values. It seems that almost any kind of binding fails. We have tried:
AllowEdit="{Binding ElementName=parentUserControlName, Path=IsEditAllowed} "
and
AllowEdit="{Binding IsEditAllowed}" after setting the User Control's DataContext to "this".
Both times we get the error:
System.Windows.Data Error: 2 : Cannot find governing FrameworkElement or FrameworkContentElement for target element. BindingExpression:Path=IsEditAllowed; DataItem=null; target element is 'FieldSettings' (HashCode=15935451); target property is 'AllowEdit' (type 'Nullable`1')
However, if we set the Binding Source to be an x:Static, it works. For example:
AllowEdit="{Binding Path=IsEditAllowed, Source={x:Static Sample:EntitlementsProvider.Instance}}" it works fine!!!
I know that FieldSettings is just a DependencyObject, not a framework element, but if anyone could explain why this is happening and what workarounds we can do to dynamically set the AllowEdit property in a consistent and extensible manner, please let me know.
Thanks,
-Szymon
I am having the same problem. I have a UserControl that hosts a XamDataGrid. I have a dependency property IsEditable that can be set on the UserControl. Based on the value of this property, I want to set AllowEdit to true or false. I have tried the following and I get the same error as mentioned above. Does anyone have a solution for this?
<igDP:FieldSettings CellWidth="25" LabelWidth="25"
AllowEdit="{Binding ElementName=ROTabGridUserControl,Path=IsEditable}"
EditorType="{x:Type igEditors:XamNumericEditor}"
EditorStyle="{StaticResource PercentageEditorStyle}"
/>
</igDP:UnboundField.Settings>
void xamDataGrid_factDefinition_FieldLayoutInitialized(object sender, FieldLayoutInitializedEventArgs e){ for (int i = 0; i < e.FieldLayout.Fields.Count; i++) { e.FieldLayout.Fields[i].Settings.AllowEdit = IsEditable; }}
Thank you. This is what I ended up doing. I was just hoping there was a way to do it in the xaml.
Is this still the case in the latest version? You can't simply do something like...
<igDP:Field Name="Price"> <igDP:Field.Settings> <igDP:FieldSettings LabelWidth="60" CellWidth="60" AllowEdit="{Binding Path=IsEditAllowed}"></igDP:FieldSettings> </igDP:Field.Settings> </igDP:Field>
Hello,
Thank your for the provided information, I believe other community members may benefit from this.
There is a work around for this problem.
Read up on BindingProxies at http://www.thomaslevesque.com/2011/03/21/wpf-how-to-bind-to-data-when-the-datacontext-is-not-inherited/
Kudos to Mr. Levesque for his novel solution.
Hello Lakshmi,
Could you please send an isolated sample project, where the issue is reproduced, so I can investigate it further for you.
Looking forward for your reply.
Hi I am Unable to edit the first Row in XamDatagrid (new row on top of the grid). Bottom rows are editiable with "IsReadOnly" property binding as below. Could please help me here to make first row as well editable according below property ASAP
<Setter Property="IsReadOnly" Value="{Binding DataContext.CanNotEditText, RelativeSource={RelativeSource AncestorType=UserControl}}" />
Yes it is still limitation. As Andrew said the FieldSettings element is not part of the visual tree, so it doesn't inherit the DataContext.