I'm using the MVVM pattern with the DevExpress Grid in Xamarin.Forms and would like to use Commanding with the Swipe Buttons (LeftSwipeButton for example). Is this possible or do I have to place the logic in the code behind file?
Using Commanding with Swipe Buttons in Xamarin.Forms Grid
Answers approved by DevExpress Support
Hi,
The best way to achieve your goal is to use the GridControl.SwipeButtonCommand commands property. The GridControl calls a command from it on swipe button taps.
Please do not hesitate to contact us if you need further assistance.
Thanks
Alex
Hi,
I agree with this article that view models shouldn't have references on UI components in MVVM applications. For example, your ViewModel shouldn't have reference on the GridControl instance and work with it directly, and of course I don't suggest you to do this :) I don't think that this request means that your ViewModel project shouldn't have references on our component libraries. If you have a correct MVVM application structure, it doesn't matter which libraries your ViewModel project references. In my suggested approach, you need to use only a little helper class from our library, not UI components, so in my opinion, this doesn't break the MVVM approach.
Let me describe why we have created this behavior. The GridControl has a lot of rows, each row can have several swipe buttons, so we need to send a minimum two values in the command: row index and which button in this row was tapped. However, commands support only one object as their parameter. So we have decided to create a helper object that contains all required data and send this object as a parameter in the command. I think this approach is the most concise and doesn't contradict with the MVVM pattern, because it allows you to get all required information without references to any UI components.
Feel free to contact me if you need additional information.
Thanks
Alex
That makes sense. I'm fairly new to MVVM so I appreciate you taking the time to explain. I'm hoping to get a chance to implement the commanding today. Take care.
This is the Xamarin List View I'll be replacing if this helps. I used Commands for the Context Actions before.
<ListView x:Name="configList" ItemsSource="{Binding ConfigsGrouped}"
IsGroupingEnabled="True"
GroupDisplayBinding="{Binding Key}"
GroupShortNameBinding="{Binding Key}"
HasUnevenRows="True"
SelectedItem="{Binding SelectedConfig, Mode=TwoWay}">
<ListView.GroupHeaderTemplate>
<DataTemplate>
<ViewCell
Height="30">
<StackLayout
Style="{StaticResource headerLayout}">
<Label
Text="{Binding Key}"
Style="{StaticResource headerLabel}" />
</StackLayout>
</ViewCell>
</DataTemplate>
</ListView.GroupHeaderTemplate>
<ListView.ItemTemplate>
<DataTemplate>
<ImageCell
x:Name="configImage"
Text="{Binding CfgNO}"
TextColor="Black"
Detail="{Binding Description}"
DetailColor="#002E55">
<ImageCell.ImageSource>
<UriImageSource Uri="{Binding ThumbnailImageUri}" CacheValidity="7"/>
</ImageCell.ImageSource>
<ImageCell.ContextActions>
<MenuItem Text="Details"
Command="{Binding OpenDetails}"/>
<MenuItem Text="Images"
Command="{Binding OpenGallery}"/>
</ImageCell.ContextActions>
</ImageCell>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>