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
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.