SfNumericUpDown ValueChanged event not working inside DataTemplate

Hi,


I'm having an issue getting an event to command linked when my SfNumericUpDown value changes.


I have a SfNumericUpDown within a Xamarin.Forms.DataTemplate and when I change SfNumericUpDown control it is not triggering my NumericValueChangedEvent method in my viewmodel. I used

EventToCommandBehavior to convert from event to command.


<StackLayout

            x:Name="CartLayout"

            Grid.Row="2"

            BindableLayout.ItemsSource="{Binding CartItems}"

            Spacing="16"

            Margin="20"

            VerticalOptions="Start">

            <BindableLayout.ItemTemplate>

                <DataTemplate>

                    <Grid

                    BackgroundColor="{DynamicResource Gray-White}"

                    ColumnSpacing="0"

                    RowSpacing="0">

                        <Grid.RowDefinitions>

                            <RowDefinition Height="Auto" />

                            <RowDefinition Height="*" />

                        </Grid.RowDefinitions>

                        <Grid.ColumnDefinitions>

                            <ColumnDefinition Width="Auto" />

                            <ColumnDefinition Width="*" />

                            <ColumnDefinition Width="*" />

                        </Grid.ColumnDefinitions>

                        <numeric:SfNumericUpDown x:Name="numericUpDown" Value="{Binding Quantity}" Margin="10" Grid.Row="0">

                            <numeric:SfNumericUpDown.Behaviors>

                                <local:EventToCommandBehavior EventName="ValueChanged" Command="{Binding QtyCommand}" CommandParameter="{x:Reference numericUpDown}"/>

                            </numeric:SfNumericUpDown.Behaviors>

                        </numeric:SfNumericUpDown>

                    </Grid>

                </DataTemplate>

            </BindableLayout.ItemTemplate>

        </StackLayout>


C#


Command<ValueEventArgs> qtyCommand;

public Command<ValueEventArgs> QtyCommand

        {

            get { return qtyCommand; }

            protected set { qtyCommand = value; }

        }

qtyCommand = new Command<Syncfusion.SfNumericUpDown.XForms.ValueEventArgs>(NumericValueChangedEvent);

public void NumericValueChangedEvent(ValueEventArgs e)

        {

            Application.Current.MainPage.DisplayAlert("Qty", "Number:", "Ok");

        }


I have attached the sample project I tried.

Any help is appreciated!!!!


Attachment: EventToCommand_b9a361cd.zip

3 Replies 1 reply marked as answer

VR Vignesh Ramesh Syncfusion Team January 27, 2022 05:21 PM UTC

Hi Kavin, 
 
We have checked the provided sample and would like to inform you that you have try to bound the command in the StackLayout’s ItemTemplate. So, the corresponding Item of the StackLayout comes as its BindingContext (ie., Contacts class). But the QtyCommand is defined in ViewModel class (ie., ContactsViewModel). So only the event to command binding is not get worked. To overcome this problem, we must change the binding context as like below snippet. 
 
[XAML]: 
 
<StackLayout x:Name="CartLayout" 
             > 
    <BindableLayout.ItemTemplate> 
        <DataTemplate> 
            <Grid BackgroundColor="{DynamicResource Gray-White}"> 
                 
                <numeric:SfNumericUpDown x:Name="numericUpDown" 
                                         Value="{Binding Quantity}" 
                                         Margin="10" 
                                         Grid.Row="0"> 
                    <numeric:SfNumericUpDown.Behaviors> 
                        <local:EventToCommandBehavior EventName="ValueChanged" 
                                                      Command="{Binding BindingContext.QtyCommand, Source={x:Reference Name=CartLayout}}"/> 
                    </numeric:SfNumericUpDown.Behaviors> 
                </numeric:SfNumericUpDown> 
            </Grid> 
        </DataTemplate> 
    </BindableLayout.ItemTemplate> 
… 
 

Please get the modified sample from the below link. 

Regards, 
Vignesh Ramesh. 


Marked as answer

KA Kavin Avinash January 27, 2022 05:52 PM UTC

Thanks, Vignesh for the quick response. Now SfNumericUpDown ValueChanged event is working inside DataTemplate after bindingcontext is added to the command.

Thanks a lot 🙏.



ET Eswaran Thirugnanasambandam Syncfusion Team January 28, 2022 06:10 AM UTC

Hi Kavin, 
 
We are glad to hear that the provided solution resolved the problem. Please get in touch with us if you would require any further assistance.  
 
Regards, 
Eswaran 


Loader.
Up arrow icon