i try to bind my "IsReadOnly"-Property to FieldSettings.AllowEdit and FieldLayoutSettings.AllowAddNew Property, with my own "OppositeNullableBoolConverter".
<igDP:XamDataGrid xmlns:igDP="http://infragistics.com/DataPresenter" DataContext="{Binding Aktivitaet}" DataSource="{Binding DataContext.rsVerteiler, RelativeSource={RelativeSource Self}}" Height="127" Margin="161,326.723,19,0" Name="grd_Verteiler" VerticalAlignment="Top"> <igDP:XamDataGrid .FieldLayoutSettings> <igDP:FieldLayoutSettings AllowAddNew="{Binding Path=DataContext.IsReadOnly, Mode=OneWay, RelativeSource={RelativeSource AncestorType={x:Type igDP:XamDataGrid}}, Converter={StaticResource localBoolToOppositeNullableBoolConverter}}" /> </igDP:XamDataGrid .FieldLayoutSettings> <igDP:XamDataGrid .FieldSettings> <igDP:FieldSettings AllowEdit="{Binding Path=DataContext.IsReadOnly, Mode=OneWay, RelativeSource={RelativeSource AncestorType={x:Type igDP:XamDataGrid}}, Converter={StaticResource localBoolToOppositeNullableBoolConverter}}" /> </igDP:XamDataGrid .FieldSettings> </igDP:XamDataGrid >
alternative binding:
<igDP:XamDataGrid xmlns:igDP="http://infragistics.com/DataPresenter" DataSource="{Binding Aktivitaet.rsVerteiler}" Height="127" Margin="161,326.723,19,0" Name="grd_Verteiler" VerticalAlignment="Top"> <igDP:XamDataGrid.FieldLayoutSettings> <igDP:FieldLayoutSettings AllowAddNew="{Binding Path=Aktivitaet.IsReadOnly, Mode=OneWay, Converter={StaticResource localBoolToOppositeNullableBoolConverter}}" /> </igDP:XamDataGrid.FieldLayoutSettings> <igDP:XamDataGrid.FieldSettings> <igDP:FieldSettings AllowEdit="{Binding Path=Aktivitaet.IsReadOnly, Mode=OneWay, Converter={StaticResource localBoolToOppositeNullableBoolConverter}}" /> </igDP:XamDataGrid.FieldSettings> </igDP:XamDataGrid>
the binding to DataSource works fine but by the binding at AllowAddNew and AllowEdit i get this binding error at runtime:
Cannot find governing FrameworkElement or FrameworkContentElement for target element
what am I doing wrong?
thank you.
Hi,
This error is usually thrown by the RelativeSource expression when the binding expression could not find an ancestor element in the visual tree - in your case I think this link of code is throwing the error:RelativeSource={RelativeSource AncestorType={x:Type igDP:XamDataGrid}}
Note that the FieldSettings object is not in the visual tree so the syntax that you are using will not work at all. You can find more details about this limitation here. Also in order the AddNewRow to appear in the grid your collection should implement IBindingList interface (e.g. BindingList). I suggest you to add a reference to your viewmodel class in the view and use it as a StaticResource when binding the AllowEdit and AllowAddNew properties. I'm attaching a sample project so you can see this implementation.
I hope this helps.
Can you please advise whether this issue is resolved in version 11.2 released last month and if not is it likely to be resolved.
As described in previous post I am also trying to change the editablity of a xamDataGrid's rows/cells based on a boolean property in the viewmodel.
I have tried your example however, creating a StaticResource of your viewmodel in your view will create a new instance of the viewmodel. This has no relation to the one created in the App start up assigned to the datacontext. Consequently changing the boolean value in the Datacontext Viewmodel has no effect on the AllowEdit property of the xamDataGrid.
Unless I have missed something here I cannot get this to work.
I have found an alternative work around by using a behaviour that triggers the change to AllowEdit property binding to a boolean property on the viewmodel...
Create a new class:
Public Class XamlDataGridBehaviors
Public Shared Function GetIsEditable(target As DependencyObject) As Object
Return DirectCast(target.GetValue(IsEditableProperty), Object)
End Function
Public Shared Sub SetIsEditable(target As DependencyObject, value As Object)
target.SetValue(IsEditableProperty, value)
End Sub
Public Shared ReadOnly IsEditableProperty As DependencyProperty = DependencyProperty.RegisterAttached("IsEditable", GetType(Object), GetType(XamlDataGridBehaviors), New UIPropertyMetadata(Nothing, AddressOf OnIsEditableChanged))
Private Shared Sub OnIsEditableChanged(target As DependencyObject, e As DependencyPropertyChangedEventArgs)
Dim val As Boolean = DirectCast(e.NewValue, Boolean)
Dim fe As FrameworkElement = DirectCast(target, FrameworkElement)
Dim control As XamDataGrid = DirectCast(target, XamDataGrid)
control.FieldSettings.AllowEdit = val
End Class
<igDP:XamDataGrid x:Name="Test" BindToSampleData="True" Margin="0,80,0,0" local:XamlDataGridBehaviors.IsEditable="{Binding Path=IsEditable, Mode=OneWay}">
Then lastly, bind the behaviour to the XamDataGrid linking to the IsEditable property in the viewmodel.
Hope this helps someone else
Regards
Craig
Hi craighoad,
Your solution works. Thanks.
Modified it for C#
public static readonly DependencyProperty IsAllowAddNewProperty =
DependencyProperty.Register(
"IsAllowAddNew", typeof(bool), typeof(BlotterGrid),
new FrameworkPropertyMetadata(false, new PropertyChangedCallback(OnIsAllowAddNewChanged), null));
public bool IsAllowAddNew
{
get { return (bool)GetValue(IsAllowAddNewProperty); }
set { SetValue(IsAllowAddNewProperty, value); }
}
private static void OnIsAllowAddNewChanged(DependencyObject obj, DependencyPropertyChangedEventArgs args)
var control = (BlotterGrid)obj;
var e = new RoutedPropertyChangedEventArgs<bool>(
(bool)args.OldValue, (bool)args.NewValue, null);
control.OnIsAllowAddNewChanged(e);
protected virtual void OnIsAllowAddNewChanged(RoutedPropertyChangedEventArgs<bool> args)
CustomBlotterGrid.FieldSettings.AllowEdit = args.NewValue;
CustomBlotterGrid.FieldLayoutSettings.AllowAddNew = args.NewValue;