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>
Yes this is still the case. Unfortunately this is a current limitation of WPF. The DataContext is only available on certain objects (e.g. FrameworkElement and elements which have an inheritance context). Unfortunately the inheritance context infrastructure within WPF is internal so we cannot access it. I would recommend rating the suggestion for adding support for this on MS' site here.
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,
Thank your for the provided information, I believe other community members may benefit from this.