I want to activate/deactivate editing on a XamGrid from the menu, usually, to toggle a Xaml property using the view model (DataContext) I use Binding and a Converter. I've tried the following code:
<ig:XamGrid.EditingSettings> <ig:EditingSettings AllowEditing="{Binding Path=IsInEditMode, Converter={StaticResource boolToEditingTypeConverter}, Mode=OneWay}" IsMouseActionEditingEnabled="DoubleClick" IsEnterKeyEditingEnabled="True" IsF2EditingEnabled="True" IsOnCellActiveEditingEnabled="False" /></ig:XamGrid.EditingSettings>
Where IsInEditMode is a property on my Viewmodel specifically a DependencyProperty.
The Converter is never called so I presume the binding path is not found. Maybe because the object path is not connected to the model but something else...
Can you tell me where I'm wrong?
thank you
Sabrina
Hi Sabrina,
Unfortunately that property cannot bind to anything in the DataContext because the EditingSettings object is not part of the visual tree. The only way to "bind" the EditingSettings.AllowEditing property to your view model's IsInEditMode property is through something like a Behavior<T> that can move the values between the two. A behavior like this should do the trick:
public class XamGridSettings : Behavior<XamGrid> { public EditingType AllowEditing { get { return (EditingType)GetValue(AllowEditingProperty); } set { SetValue(AllowEditingProperty, value); } } // Using a DependencyProperty as the backing store for AllowEditing. This enables animation, styling, binding, etc... public static readonly DependencyProperty AllowEditingProperty = DependencyProperty.Register("AllowEditing", typeof(EditingType), typeof(XamGridSettings), new PropertyMetadata(EditingType.None, OnAllowEditingChanged)); private static void OnAllowEditingChanged(DependencyObject d, DependencyPropertyChangedEventArgs e) { // The view model property was updated so update the XamGrid XamGridSettings settings = d as XamGridSettings; settings.AssociatedObject.EditingSettings.AllowEditing = settings.AllowEditing; } protected override void OnAttached() { AssociatedObject.EditingSettings.PropertyChanged += EditingSettings_PropertyChanged; } void EditingSettings_PropertyChanged(object sender, System.ComponentModel.PropertyChangedEventArgs e) { // The XamGrid property was updated, so update the view model property. if (e.PropertyName == "AllowEditing") AllowEditing = AssociatedObject.EditingSettings.AllowEditing; } }
<ig:XamGrid ...> <i:Interaction.Behaviors> <behaviors:XamGridSettings AllowEditing="{Binding Path=IsInEditMode, Converter={StaticResource boolToEditingTypeConverter}, Mode=OneWay}"/> </i:Interaction.Behaviors> </ig:XamGrid>
Nice bit of code, what assembly and what namespace got the Behavior class? and which is the Interaction namespace?
thank you in advance
The assembly is System.Windows.Interactivity.dll and the namespace Behavior<T> is part of is "System.Windows.Interactivity". In the XAML, i:Interaction comes from: xmlns:i="http://schemas.microsoft.com/expression/2010/interactivity"
If you are using Visual Studio 2012 or later that DLL should come with it. If you are using Visual Studio 2010 then you need to install the Expression Blend SDK to get it. There is a nuget package for the dll though so this would probably be quicker to use if you have Visual Studio 2010 http://www.nuget.org/packages/System.Windows.Interactivity.WPF/
Let me know if you have any further questions on this matter.