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

MVVM -Binding Listview from viewmodel

$
0
0

help plz. when i run my app the screen is empty .why ?? here my code
<ContentPage.ToolbarItems >

    </ToolbarItem>
</ContentPage.ToolbarItems>

<ContentPage.Content>
    <StackLayout Padding="10,0,10,0">
        <Label Text="Customers"/>
        <SearchBar TextChanged="Handle_TextChanged"></SearchBar>
        <ListView   x:Name="EmployeeListView" ItemsSource="{Binding MyListCollector}" IsPullToRefreshEnabled="True"
                   IsGroupingEnabled="True" GroupDisplayBinding="{Binding Name}" 
                   HasUnevenRows="true" SeparatorVisibility="Default" >
            <ListView.ItemTemplate>
                <DataTemplate>
                    <ViewCell>
                        <StackLayout Orientation="Horizontal">
                            <Image Source="{Binding Image}" HeightRequest="50" WidthRequest="50"  />
                            <StackLayout HorizontalOptions="StartAndExpand">
                                <Label Text="{Binding Name}"
                                   FontAttributes="Bold" />
                                <Label Text="{Binding MyDetail}"
                                   TextColor="Gray" />
                            </StackLayout>
                            <Button Text="View"
                                BorderColor="Silver"
                               FontSize="Small"
                               TextColor="White"
                               BackgroundColor="#3399ff"
                               VerticalOptions="Center"
                               Clicked="Handle_ClickedAsync" />
                        </StackLayout>
                    </ViewCell>
                </DataTemplate>
            </ListView.ItemTemplate>
        </ListView>

    </StackLayout>

</ContentPage.Content>

namespace Customers.Views
{
[XamlCompilation(XamlCompilationOptions.Compile)]
public partial class MyListPage : ContentPage
{
public MyListPage()
{
InitializeComponent();

        BindingContext = new MyListeViewModel();

    }

    void Handle_TextChanged(object sender, Xamarin.Forms.TextChangedEventArgs e)
    {
        var _container = BindingContext as MyListeViewModel;
        EmployeeListView.BeginRefresh();

        if (string.IsNullOrWhiteSpace(e.NewTextValue))
            EmployeeListView.ItemsSource = _container.MyListCollector;
        else
            EmployeeListView.ItemsSource = _container.MyListCollector.Where(i => i.Name.Contains(e.NewTextValue));

        EmployeeListView.EndRefresh();
    }


    async void Handle_ClickedAsync(object sender, System.EventArgs e)
    {
        var _container = BindingContext as MyListeViewModel;

        await Navigation.PushModalAsync(new NavigationPage(new MyListViewDetailPage()));


    }
    void AddItem_Clicked(object sender, System.EventArgs e)
    {
        var _container = BindingContext as MyListeViewModel;

        DisplayAlert("Sucess", "ADD success", "OK", "Cancel");
    }
}

}
namespace Customers.ViewModel
{
public class MyListeViewModel
{
public ObservableCollection MyListCollector { get; set; }

    public MyListeViewModel()
    {

        MyListCollector = new ObservableCollection<MyListModel>()
        {
             new MyListModel(){ Name="BKonsult" , MyDetail=" Details" , Image = "Client.png" },
            new MyListModel(){ Name="ssh" ,   MyDetail="Details" , Image = "Client.png"},
            new MyListModel(){ Name="Ardia" , MyDetail="Details ",Image="Client.png"},
            new MyListModel(){ Name="Delice" , MyDetail="Details ",Image="Client.png"},

};}
namespace Customers.Model
{
public class MyListModel {
public string Name { get; set; }
public string MyDetail { get; set; }
public string Image { get; set; }
public string Phone { get; set; }
public string Mail { get; set; }
public string Adress { get; set; }
public App()
{
InitializeComponent();

        MainPage = new NavigationPage(new MyListPage());
    }

Viewing all articles
Browse latest Browse all 77050

Trending Articles



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