Quantcast
Channel: Xamarin.Forms — Xamarin Community Forums
Viewing all articles
Browse latest Browse all 58056

Xamarin Forms - Issue using ViewModel with Commands and View with SetBinding to ItemSelected.

$
0
0

Please find below code that I have written in an attempt to use a viewmodel with Command method, which then is connected to list.SetBinding, which doesnt work

ViewModel

public class TestViewModel1 : ITestViewModel
{

    public ICommand ItemSelected
    {
        get
        {
            return new Command(() => { ExecuteCommand(); });
        }
    }

    public ICommand ItemTapped
    {
        get
        {
            return new Command(() => { ExecuteCommand(); });
        }
    }


    private CalculationView _cv;

    public List<string> Models { get; set; }
    public TestViewModel1(CalculationView cv)
    {
        Models = new List<string>();
        _cv = cv;

        List<string> test1 = new List<string>();
        test1.Add("Number 1");
        test1.Add("Number 1a");
        test1.Add("Number 1b");

        Models = test1;
    }

    protected void ExecuteCommand()
    {
        var masterpage = (TabbedPage)_cv.Parent;
        masterpage.CurrentPage = masterpage.Children[1];
    }
}

View

public class ListPage : ContentPage
{

    private ITestViewModel _vm;
    public ListPage(ITestViewModel testvm)
    {

        _vm = testvm;

        BindingContext = testvm;

        var list = new ListView();

        list.ItemsSource = _vm.Models;

        //list.ItemSelected += (sender, e) =>
        //{
        //    var masterpage = (TabbedPage)this.Parent;

        //     masterpage.CurrentPage = masterpage.Children[1];

        //};

        list.SetBinding(ListView.SelectedItemProperty, new Binding("ItemSelected", BindingMode.OneWay));

        var stack = new StackLayout { Orientation = StackOrientation.Horizontal };
        stack.Children.Add(list);

        //var scv = new ScrollView { Content = stack, HorizontalOptions = LayoutOptions.FillAndExpand, Orientation = ScrollOrientation.Horizontal  };
        Content = stack;
    }


}

TabbedPage

public class CalculationView : TabbedPage
{

    public CalculationView()
    {
        Title = "test";

        Children.Add(new ListPage(new TestViewModel1(this)));
        Children.Add(new ListPage(new TestViewModel2(this)));
        Children.Add(new ListPage(new TestViewModel3(this)));

        this.Icon = "Boltup Mobile";

    }

}

Im sure there is something basic which Im missing. Sorry, but ive looked around and can't seem to find a definitive answer...

thanks in anticipation


Viewing all articles
Browse latest Browse all 58056

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>