Ticket T300361
Visible to All Users

Using Commanding with Swipe Buttons in Xamarin.Forms Grid

created 10 years ago

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?

Comments (1)
P P
PeerlessProgrammer 10 years ago

    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>

    Answers approved by DevExpress Support

    created 10 years ago

    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

      Show previous comments (8)
      DevExpress Support Team 10 years ago

        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

        P P
        PeerlessProgrammer 10 years ago

          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.

          DevExpress Support Team 10 years ago

            Hi,

            You are welcome! I will be glad to help you any time.

            Thanks
            Alex

            Disclaimer: The information provided on DevExpress.com and affiliated web properties (including the DevExpress Support Center) is provided "as is" without warranty of any kind. Developer Express Inc disclaims all warranties, either express or implied, including the warranties of merchantability and fitness for a particular purpose. Please refer to the DevExpress.com Website Terms of Use for more information in this regard.

            Confidential Information: Developer Express Inc does not wish to receive, will not act to procure, nor will it solicit, confidential or proprietary materials and information from you through the DevExpress Support Center or its web properties. Any and all materials or information divulged during chats, email communications, online discussions, Support Center tickets, or made available to Developer Express Inc in any manner will be deemed NOT to be confidential by Developer Express Inc. Please refer to the DevExpress.com Website Terms of Use for more information in this regard.