Forum Xamarin.Forms
We are excited to announce that the Xamarin Forums are moving to the new Microsoft Q&A experience. Q&A is the home for technical questions and answers at across all products at Microsoft now including Xamarin!

We encourage you to head over to Microsoft Q&A for .NET for posting new questions and get involved today.

How to call Command from DataTemplate Entry TextChanged Event in Xamarin Forms?

ScumSprocketScumSprocket Member ✭✭
edited October 25 in Xamarin.Forms

Using DataTemplates in a CollectionView... If I can call a ViewModel's Command from a button like this:

<Button Text="Test"
    Command="{Binding Source={RelativeSource AncestorType={x:Type ContentPage}},
                                           Path=BindingContext.TestCommand}"/>

Why can't I call that command from an Entry's TextChanged Event like this?

<Entry x:Name="PortionEntry"
    Text ="{Binding QtyTest, Mode=TwoWay}">
    <Entry.Behaviors>
        <behavors:EventToCommandBehavior
            EventName="TextChanged"
            Command="{Binding Source={RelativeSource AncestorType={x:Type ContentPage}},
                          Path=BindingContext.TestCommand}"/>
</Entry.Behaviors>  

My code for EventToCommandBehavior works fine when not used in a DataTemplate

I got the button code form these fine folks: https://www.syncfusion.com/kb/11029/how-to-bind-command-from-viewmodel-to-external-itemtemplate-of-xamarin-forms-listviewhttps://www.syncfusion.com/kb/11029/how-to-bind-command-from-viewmodel-to-external-itemtemplate-of-xamarin-forms-listview

Thanks!

Answers

  • ScumSprocketScumSprocket Member ✭✭

    The scenario is, I have Entries on the DatTemplates that I'd like to capture the TextChanged event from.

  • ColeXColeX Member, Xamarin Team Xamurai
    edited October 27

    Could you post the viewmodel code ?

    Make sure the binding path is correct , here is a simple sample ,

        public class Model
        {
            public string QtyTest { get; set; }
    
            public ICommand TestCommand { get; set; }
    
            public Model()
            {
                TestCommand = new Command(myMethod);
            }
    
            void myMethod()
            {
    
            }
        }
    
      [XamlCompilation(XamlCompilationOptions.Compile)]
        public partial class Page2 : ContentPage
        {
    
            List<Model> list = new List<Model>();
            public Page2()
            {
                InitializeComponent();
    
                list.Add(new Model { QtyTest = "1" });
                list.Add(new Model { QtyTest = "2" });
                list.Add(new Model { QtyTest = "3" });
    
                listView.ItemsSource = list;
    
            }
        }
    
     <ListView x:Name="listView" HasUnevenRows="True" RowHeight="100">
                <ListView.ItemTemplate>
                    <DataTemplate>
                        <ViewCell >
                            <StackLayout>
                                <Button Text="click" Command="{Binding TestCommand}"/>
                                <Entry Text ="{Binding QtyTest, Mode=TwoWay}">
                                    <Entry.Behaviors>
                                        <local:EventToCommandBehavior
                                            EventName="TextChanged"
                                            Command="{Binding TestCommand}"/>
                                    </Entry.Behaviors>
                                </Entry>
                            </StackLayout>
                        </ViewCell>
                    </DataTemplate>
                </ListView.ItemTemplate>
            </ListView>
    
  • ScumSprocketScumSprocket Member ✭✭

    Thanks, but it's not a ContentPage, it's DataTemplate. I'll post a sample project, very soon.

  • ColeXColeX Member, Xamarin Team Xamurai

    @ScumSprocket said:
    Thanks, but it's not a ContentPage, it's DataTemplate. I'll post a sample project, very soon.

    ok :)

  • ScumSprocketScumSprocket Member ✭✭

    @ColeX Somehow I have gotten 2 threads going by mistake, and I'm not sure how to delete this one:. Sample project posted here:
    https://forums.xamarin.com/discussion/comment/424254#Comment_424254

Sign In or Register to comment.