Hi there
I’m using WPF Infragistic 2011 V2 XamDatagrid. The application is based on MVVM. I need to prevent some records from being deleted based on a Boolean property (IsReadOnly) in my model.
I have tried to bind AllowDelete property either to a bool in my model or a to property in a VM:
<igDP:XamDataGrid.FieldLayoutSettings>
<igDP:FieldLayoutSettings AutoGenerateFields="False" AllowDelete="{Binding ....." />
</igDP:XamDataGrid.FieldLayoutSettings>
But in both cases I'm getting runtime error "Cannot find governing FrameworkElement or FrameworkContentElement for target element. BindingExpression:Path= "
How can I stop a user from deleting data rows selectively without using code behind?
RegardsBoris
If you are using MVVM you want to take a different approach for handling deletes. Don't rely on the grid to do it for you. Check out the attached sample.
Not sure about popping up a Message box from VM... but how about deriving from XamDataGrid and adding AllowDelete DependencyProperty?
public class XamDataGridEx : XamDataGrid{
public static readonly DependencyProperty AllowDeleteProperty = DependencyProperty.Register("AllowDelete", typeof(bool),
typeof(XamDataGridEx), new UIPropertyMetadata(false, OnAllowDeletePropertyChanged));
private static void OnAllowDeletePropertyChanged(DependencyObject source, DependencyPropertyChangedEventArgs e)
{
XamDataGridEx control = source as XamDataGridEx;
bool value = (bool)e.NewValue;
control.FieldLayoutSettings.AllowDelete = value;
}
public bool AllowDelete
get { return (bool)GetValue(AllowDeleteProperty);}
set { SetValue(AllowDeleteProperty, value);}
Thank you Brian.
Your approach will work. So would the solution suggested by Stefan. Personally I would probably prefer your suggestion as it does not involve a use of Interactions so there are fewer concepts to grasp. But let’s assume for a moment that I already have a need to derive from XamDataGrid to modify its behavior in some other areas of functionality. AllowDelete property is implemented in FieldLayoutSettings class. In most cases there is only one instance of FieldLayoutSettings so it appears that implementing AllowDelete in XamDataGrid derived class would be semantically equivalent to implementing it on FieldLayoutSettings. And this is where you have lost me. What is your rational in suggesting to implement it as an Attached Property? Which object would be the property owner in this case? Perhaps you could put together a simple example.
The sample project that I have uploaded does not quite work. It appears that AllowDelete condition is evaluated before the ActiveDataItem bound property is set in VM. As a result while AllowDelete property changed event is sent and AllowDelete bound property in VM is set correctly, the grid uses the value next time when the user hits DEL key. Please run the sample and you’ll know what I mean. Not sure if it can be viewed as a bug in XamDataGrid considering that AllowDelete was never intended to be used that way. May be implementing it as an Attached property would help…
To summarize this rather long post there are two questions:
On a different subject – accidently stumbled across your article about XamGantt charts. It has saved us enormous amount of efforts. Big thank you.
You don't have to use a MessageBox. Instead you would use some type of IDialogService. The sample was only meant to show you how to perform a delete using MVVM. I would not recommend deriving from the XamDataGrid for this purpose. This would only add complexity to your code base, but it also only applies to the entire grid and not a row by row scenario. Also, even if you decided to have such a property, you would be better off using an AttachedProperty instead.