来自codebehind的DataTemplate中绑定listview ItemSource - xamarin表单c#

时间:2017-03-15 08:39:44

标签: c# listview binding xamarin.forms portable-class-library

我需要将listview与数据绑定。 问题是这个listview在DataTemplate中,我不能从codebehind中调用它。 在WPF中,我会从发送者那里绘制对象,但是Xamarin表单将不起作用。 我试图使用ObservableCollection,但不应该是相同的,有人有建议吗?

<ContentPage.Content>
    <Grid x:Name="griglia">
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="*"></ColumnDefinition>
        </Grid.ColumnDefinitions>
        <Grid.RowDefinitions>
            <RowDefinition Height="10*"></RowDefinition>
            <RowDefinition Height="80*"></RowDefinition>
            <RowDefinition Height="8*"></RowDefinition>
            <RowDefinition Height="12*"></RowDefinition>
        </Grid.RowDefinitions>
        <Grid Padding="7" Grid.Row="0">
            <Grid.ColumnDefinitions>
                <ColumnDefinition Width="15*"></ColumnDefinition>
                <ColumnDefinition Width="61*"></ColumnDefinition>
                <ColumnDefinition Width="6*"></ColumnDefinition>
                <ColumnDefinition Width="18*"></ColumnDefinition>
            </Grid.ColumnDefinitions>
            <Grid.RowDefinitions>
                <RowDefinition Height="*"></RowDefinition>
            </Grid.RowDefinitions>
            <Image Grid.Row="0" Grid.Column="0" x:Name="littleLogo"></Image>
            <Label Grid.Row="0" Grid.Column="1" Text="Home" FontSize="Medium" VerticalTextAlignment="Center" TextColor="#fff"></Label>
            <StackLayout VerticalOptions="Center" Grid.Row="0" Grid.Column="2">
                <Image HeightRequest="20" Aspect="AspectFit" x:Name="littleMen"></Image>
            </StackLayout>
            <Label Grid.Row="0" Grid.Column="3" x:Name="logoutLabel" Text="Logout" FontSize="Medium" HorizontalTextAlignment="Start" VerticalTextAlignment="Center" TextColor="#fff"></Label>
        </Grid>
        <StackLayout x:Name="stackMap" Grid.Row="1">
            <!--PORZIONE CENTRALE DELLA PAGINA-->
            <!--PORZIONE CENTRALE DELLA PAGINA-->
            <!--PORZIONE CENTRALE DELLA PAGINA-->
            <!--PORZIONE CENTRALE DELLA PAGINA-->
            <!--PORZIONE CENTRALE DELLA PAGINA-->
            <ActivityIndicator x:Name="loading" IsVisible="true" Color="#fff" IsRunning="true" />
            <!--<ActivityIndicator Grid.Column="1" x:Name="loading" IsVisible="true" Color="#fff" IsRunning="true" />-->
            <Grid>
                <Grid.ColumnDefinitions>
                    <ColumnDefinition Width="*"></ColumnDefinition>
                </Grid.ColumnDefinitions>
                <Grid.RowDefinitions>
                    <RowDefinition Height="13*"></RowDefinition>
                    <RowDefinition Height="44*"></RowDefinition>
                    <RowDefinition Height="0.15*"></RowDefinition>
                    <!-- linea bianca che divide le due porzioni di pagina, sopra foto macchina e sotto lo slide dei dati -->
                    <RowDefinition Height="42.85*"></RowDefinition>
                </Grid.RowDefinitions>
                <!-- griglia per la freccia e la scritta selezione macchina -->
                <Grid Padding="7" Grid.Row="0">
                    <Grid.ColumnDefinitions>
                        <ColumnDefinition Width="15*"></ColumnDefinition>
                        <ColumnDefinition Width="60*"></ColumnDefinition>
                        <ColumnDefinition Width="25*"></ColumnDefinition>
                    </Grid.ColumnDefinitions>
                    <Grid.RowDefinitions>
                        <RowDefinition Height="*"></RowDefinition>
                    </Grid.RowDefinitions>
                    <Image Aspect="AspectFit" x:Name="backArrow" Grid.Row="0" Grid.Column="0" Source="backArrow.png"></Image>
                    <Label x:Name="selezioneMacchina" Text="Selezione macchina" Grid.Row="0" VerticalTextAlignment="Center" Grid.Column="1" TextColor="#acabac" FontSize="Small"></Label>
                    <Image Aspect="AspectFit" x:Name="settingsImage" HeightRequest="60" Grid.Row="0" Grid.Column="2" Source="settingsTechDealer.png"></Image>
                </Grid>
                <!-- griglia foto macchina e descrizioni a destra -->
                <Grid Padding="7" Grid.Row="1">
                    <Grid.ColumnDefinitions>
                        <ColumnDefinition Width="60*"></ColumnDefinition>
                        <ColumnDefinition Width="40*"></ColumnDefinition>
                    </Grid.ColumnDefinitions>
                    <Grid.RowDefinitions>
                        <RowDefinition Height="*"></RowDefinition>
                    </Grid.RowDefinitions>
                    <Image x:Name="fotoMachine" Aspect="AspectFit" Grid.Row="0" Grid.Column="0"></Image>
                    <Grid Padding="7" Grid.Column="1">
                        <Grid.ColumnDefinitions>
                            <ColumnDefinition Width="*"></ColumnDefinition>
                        </Grid.ColumnDefinitions>
                        <Grid.RowDefinitions>
                            <RowDefinition Height="*"></RowDefinition>
                            <RowDefinition Height="*"></RowDefinition>
                            <RowDefinition Height="*"></RowDefinition>
                            <RowDefinition Height="*"></RowDefinition>
                            <RowDefinition Height="*"></RowDefinition>
                            <RowDefinition Height="*"></RowDefinition>
                            <RowDefinition Height="*"></RowDefinition>
                        </Grid.RowDefinitions>
                        <Label x:Name="modelloSetLabel" Text="-" Grid.Row="0" VerticalTextAlignment="Center" TextColor="#acabac" FontSize="Small"></Label>
                        <Label x:Name="cantiereLabel" Text="Cantiere" Grid.Row="1" VerticalTextAlignment="Center" TextColor="#5d666f" FontSize="Small"></Label>
                        <Label x:Name="cantiereSetLabel" Text="-" Grid.Row="2" VerticalTextAlignment="Center" TextColor="#acabac" FontSize="Small"></Label>
                        <Label x:Name="numeroMatricolaLabel" Text="numero matricola" Grid.Row="3" VerticalTextAlignment="Center" TextColor="#5d666f" FontSize="Small"></Label>
                        <Label x:Name="matricolaSerialeLabel" Text="-" Grid.Row="4" VerticalTextAlignment="Center" TextColor="#acabac" FontSize="Small"></Label>
                        <Label x:Name="prossimaManutenzione" Text="prossima manutenzione" Grid.Row="5" VerticalTextAlignment="Center" TextColor="#5d666f" FontSize="Small"></Label>
                        <Label x:Name="prossimaManutenzioneDateTrue" Text="-" Grid.Row="6" VerticalTextAlignment="Center" TextColor="#acabac" FontSize="Small"></Label>
                    </Grid>
                </Grid>
                <!-- griglia fare la riga bianca -->
                <Grid Padding="10" BackgroundColor="White" Grid.Row="2">
                    <Grid.ColumnDefinitions>
                        <ColumnDefinition Width="*"></ColumnDefinition>
                    </Grid.ColumnDefinitions>
                    <Grid.RowDefinitions>
                        <RowDefinition Height="*"></RowDefinition>
                    </Grid.RowDefinitions>
                </Grid>
                <!-- slider contenuti carousel -->
                <!-- da fare qui -->
                <Grid Grid.Row="3">
                    <Grid.ColumnDefinitions>
                        <ColumnDefinition Width="*"></ColumnDefinition>
                    </Grid.ColumnDefinitions>
                    <Grid.RowDefinitions>
                        <RowDefinition Height="*"></RowDefinition>
                    </Grid.RowDefinitions>
                    <ContentView HorizontalOptions="FillAndExpand" VerticalOptions="FillAndExpand">
                        <!-- CAROUSEL VIEW QUI  -->
                        <control:CarouselView x:Name="carouselView">
                            <control:CarouselView.ItemTemplate>
                                <DataTemplate>
                                    <Grid>
                                        <Grid.ColumnDefinitions>
                                            <ColumnDefinition Width="*"></ColumnDefinition>
                                        </Grid.ColumnDefinitions>
                                        <Grid.RowDefinitions>
                                            <RowDefinition Height="46*"></RowDefinition>
                                            <RowDefinition Height="0.2*"></RowDefinition>
                                            <RowDefinition Height="53.8*"></RowDefinition>
                                        </Grid.RowDefinitions>
                                        <!-- griglia del carousel, tutta la struttura inizia qui-->
                                        <Grid Grid.Row="0">
                                            <Grid.ColumnDefinitions>
                                                <ColumnDefinition Width="10*"></ColumnDefinition>
                                                <ColumnDefinition Width="35*"></ColumnDefinition>
                                                <ColumnDefinition Width="45*"></ColumnDefinition>
                                                <ColumnDefinition Width="10*"></ColumnDefinition>
                                            </Grid.ColumnDefinitions>
                                            <Grid.RowDefinitions>
                                                <RowDefinition Height="*"></RowDefinition>
                                            </Grid.RowDefinitions>
                                            <Button Image="backBlueArrow.png" BorderColor="Transparent" BackgroundColor="Transparent" Clicked="clickmePrev" Grid.Column="0"></Button>
                                            <!--<Image Source="backBlueArrow.png" x:Name="backClickArrow" Aspect="AspectFit" Grid.Column="0"></Image>-->
                                            <StackLayout Margin="0,0,10,0" VerticalOptions="Center" HorizontalOptions="End" Grid.Column="1">
                                                <Image HeightRequest="70" Source="{Binding Immagine}" Aspect="AspectFit"></Image>
                                            </StackLayout>
                                            <!-- griglia superiore, riprende i dati tra le barre bianche-->
                                            <Grid Grid.Row="0" Grid.Column="2">
                                                <Grid.ColumnDefinitions>
                                                    <ColumnDefinition Width="*"></ColumnDefinition>
                                                </Grid.ColumnDefinitions>
                                                <Grid.RowDefinitions>
                                                    <RowDefinition Height="40*"></RowDefinition>
                                                    <RowDefinition Height="60*"></RowDefinition>
                                                </Grid.RowDefinitions>
                                                <Label Text="{Binding Descrizione}" Margin="{Binding Margine}" TextColor="#fff" FontSize="Small" VerticalTextAlignment="End" HorizontalTextAlignment="Start" Grid.Row="0"/>
                                                <Label Text="{Binding Valore}" FontSize="Large" TextColor="#fff" VerticalTextAlignment="Start" HorizontalTextAlignment="Start" Grid.Row="1"/>
                                                <Image Margin="-25,-5,0,0" Source="{Binding ImmagineBatteriaEmoticon}" HeightRequest="50" x:Name="emoticonBattery" Grid.Row="1" VerticalOptions="Start" HorizontalOptions="Start"></Image>
                                            </Grid>
                                            <Button Image="nextBlueArrow.png" BorderColor="Transparent" BackgroundColor="Transparent" Clicked="clickmeNext" Grid.Column="3"></Button>
                                            <!--<Image Source="nextBlueArrow.png" x:Name="nextClickArrow" Aspect="AspectFit" Grid.Column="3"></Image>-->
                                        </Grid>
                                        <!-- griglia fare la riga bianca -->
                                        <Grid BackgroundColor="White" Grid.Row="1">
                                            <Grid.ColumnDefinitions>
                                                <ColumnDefinition Width="*"></ColumnDefinition>
                                            </Grid.ColumnDefinitions>
                                            <Grid.RowDefinitions>
                                                <RowDefinition Height="*"></RowDefinition>
                                            </Grid.RowDefinitions>
                                        </Grid>
                                        <!-- griglia inferiore, ritrare i dati dei testi -->
                                        <!-- QUESTA E' LA PARTE DEI GRAFICI A COLONNA, VIENE VISUALIZZATA SOLO NELL'ULTIMO SLIDE-->
                                        <RelativeLayout x:Name="test" IsVisible="{Binding stackLayout}" Grid.Row="2" BackgroundColor="#CCF4CC">
                                            <!--<Label Text="ciao" TextColor="White"></Label>-->
                                            <ListView SeparatorVisibility="None" x:Name="listView" RowHeight="55" IsPullToRefreshEnabled="False" SeparatorColor="Black" BackgroundColor="Aqua">
                                                <ListView.ItemTemplate>
                                                    <DataTemplate>
                                                        <ViewCell>
                                                            <StackLayout HeightRequest="55">
                                                                <Grid Padding="0" ColumnSpacing="0" RowSpacing="0">
                                                                    <Grid.ColumnDefinitions>
                                                                        <ColumnDefinition Width="*"></ColumnDefinition>
                                                                    </Grid.ColumnDefinitions>
                                                                    <Grid.RowDefinitions>
                                                                        <RowDefinition Height="*"></RowDefinition>
                                                                    </Grid.RowDefinitions>
                                                                    <Image Grid.Row="0" Aspect="Fill" Source="backgroundListView.png"></Image>
                                                                    <Grid Grid.Row="0">
                                                                        <Grid.ColumnDefinitions>
                                                                            <ColumnDefinition Width="50*"></ColumnDefinition>
                                                                            <ColumnDefinition Width="10*"></ColumnDefinition>
                                                                            <ColumnDefinition Width="30*"></ColumnDefinition>
                                                                            <ColumnDefinition Width="10*"></ColumnDefinition>
                                                                        </Grid.ColumnDefinitions>
                                                                        <Grid.RowDefinitions>
                                                                            <RowDefinition Height="*"></RowDefinition>
                                                                        </Grid.RowDefinitions>
                                                                        <Label Grid.Column="0" Margin="10,0,0,0" FontSize="Medium" HorizontalTextAlignment="Start" VerticalTextAlignment="Center" Text="gfhghgh" TextColor="#000" />
                                                                        <Label Grid.Column="1" Margin="10,0,0,0" FontSize="Small" HorizontalTextAlignment="Start" VerticalTextAlignment="Center" Text="fghghghg" TextColor="#000" />
                                                                    </Grid>
                                                                </Grid>
                                                            </StackLayout>
                                                        </ViewCell>
                                                    </DataTemplate>
                                                </ListView.ItemTemplate>
                                            </ListView>
                                        </RelativeLayout>
                                        <!-- QUESTA E' LA PARTE DEI DATI IN TUTTE LE ALTRE VIEW -->
                                        <ScrollView Grid.Row="2" IsVisible="{Binding notStackLayout}">
                                            <Grid Padding="5" Grid.Row="2">
                                                <Grid.ColumnDefinitions>
                                                    <ColumnDefinition Width="65*"></ColumnDefinition>
                                                    <ColumnDefinition Width="35*"></ColumnDefinition>
                                                </Grid.ColumnDefinitions>
                                                <Grid.RowDefinitions>
                                                    <RowDefinition Height="*"></RowDefinition>
                                                    <RowDefinition Height="*"></RowDefinition>
                                                    <RowDefinition Height="*"></RowDefinition>
                                                    <RowDefinition Height="*"></RowDefinition>
                                                    <RowDefinition Height="*"></RowDefinition>
                                                    <RowDefinition Height="*"></RowDefinition>
                                                    <RowDefinition Height="*"></RowDefinition>
                                                    <RowDefinition Height="*"></RowDefinition>
                                                </Grid.RowDefinitions>
                                                <Label HorizontalTextAlignment="Start" IsVisible="{Binding notStackLayout}" TextColor="#5B6667" Grid.Column="0" Grid.Row="0" Text="{Binding Testo1}"></Label>
                                                <Label HorizontalTextAlignment="End" IsVisible="{Binding notStackLayout}" TextColor="#5B6667" Grid.Column="1" Grid.Row="0" Text="{Binding Valore1}"></Label>
                                                <Label HorizontalTextAlignment="Start" IsVisible="{Binding notStackLayout}" TextColor="#5B6667" Grid.Column="0" Grid.Row="1" Text="{Binding Testo2}"></Label>
                                                <Label HorizontalTextAlignment="End" IsVisible="{Binding notStackLayout}" TextColor="#5B6667" Grid.Column="1" Grid.Row="1" Text="{Binding Valore2}"></Label>
                                                <Label HorizontalTextAlignment="Start" IsVisible="{Binding notStackLayout}" TextColor="#5B6667" Grid.Column="0" Grid.Row="2" Text="{Binding Testo3}"></Label>
                                                <Label HorizontalTextAlignment="End" IsVisible="{Binding notStackLayout}" TextColor="#5B6667" Grid.Column="1" Grid.Row="2" Text="{Binding Valore3}"></Label>
                                                <Label HorizontalTextAlignment="Start" IsVisible="{Binding notStackLayout}" TextColor="#5B6667" Grid.Column="0" Grid.Row="3" Text="{Binding Testo4}"></Label>
                                                <Label HorizontalTextAlignment="End" IsVisible="{Binding notStackLayout}" TextColor="#5B6667" Grid.Column="1" Grid.Row="3" Text="{Binding Valore4}"></Label>
                                                <Label HorizontalTextAlignment="Start" IsVisible="{Binding notStackLayout}" TextColor="#5B6667" Grid.Column="0" Grid.Row="4" Text="{Binding Testo5}"></Label>
                                                <Label HorizontalTextAlignment="End" IsVisible="{Binding notStackLayout}" TextColor="#5B6667" Grid.Column="1" Grid.Row="4" Text="{Binding Valore5}"></Label>
                                                <Label HorizontalTextAlignment="Start" IsVisible="{Binding notStackLayout}" TextColor="#5B6667" Grid.Column="0" Grid.Row="5" Text="{Binding Testo6}"></Label>
                                                <Label HorizontalTextAlignment="End" IsVisible="{Binding notStackLayout}" TextColor="#5B6667" Grid.Column="1" Grid.Row="5" Text="{Binding Valore6}"></Label>
                                                <Label HorizontalTextAlignment="Start" IsVisible="{Binding notStackLayout}" TextColor="#5B6667" Grid.Column="0" Grid.Row="6" Text="{Binding Testo7}"></Label>
                                                <Label HorizontalTextAlignment="End" IsVisible="{Binding notStackLayout}" TextColor="#5B6667" Grid.Column="1" Grid.Row="6" Text="{Binding Valore7}"></Label>
                                                <Label HorizontalTextAlignment="Start" IsVisible="{Binding notStackLayout}" TextColor="#5B6667" Grid.Column="0" Grid.Row="7" Text="{Binding Testo8}"></Label>
                                                <Label HorizontalTextAlignment="End" IsVisible="{Binding notStackLayout}" TextColor="#5B6667" Grid.Column="1" Grid.Row="7" Text="{Binding Valore8}"></Label>
                                            </Grid>
                                        </ScrollView>
                                    </Grid>
                                    <!--<Label Text="{Binding Testo}" />-->
                                </DataTemplate>
                            </control:CarouselView.ItemTemplate>
                        </control:CarouselView>
                    </ContentView>
                </Grid>
            </Grid>
            <!--FINE PORZIONE CENTRALE DELLA PAGINA-->
            <!--FINE PORZIONE CENTRALE DELLA PAGINA-->
            <!--FINE PORZIONE CENTRALE DELLA PAGINA-->
            <!--FINE PORZIONE CENTRALE DELLA PAGINA-->
            <!--FINE PORZIONE CENTRALE DELLA PAGINA-->
        </StackLayout>
        <Grid Grid.Row="2">
            <Grid.ColumnDefinitions>
                <ColumnDefinition Width="*"></ColumnDefinition>
            </Grid.ColumnDefinitions>
            <Grid.RowDefinitions>
                <RowDefinition Height="*"></RowDefinition>
            </Grid.RowDefinitions>
            <Image Aspect="Fill" Source="backgroundMenuDown.png"></Image>
            <Button x:Name="bottoneTextDatiUltimoIntervento" BackgroundColor="Transparent" Clicked="clickDatiUltimoIntervento" BorderColor="Transparent" BorderRadius="0" BorderWidth="0" TextColor="#3a81c5" FontSize="Medium" Text="DATI ULTIMO INTERVENTO"></Button>
        </Grid>
        <Image Grid.Row="3" Aspect="Fill" Source="backgroundMenuDown.png"></Image>
        <Grid Grid.Row="3">
            <Grid.ColumnDefinitions>
                <ColumnDefinition Width="33*"></ColumnDefinition>
                <ColumnDefinition Width="33*"></ColumnDefinition>
                <ColumnDefinition Width="34*"></ColumnDefinition>
            </Grid.ColumnDefinitions>
            <Grid.RowDefinitions>
                <RowDefinition Height="*"></RowDefinition>
            </Grid.RowDefinitions>
            <Image Grid.Column="0" Aspect="AspectFit" x:Name="locationDown"></Image>
            <Image Grid.Column="1" Aspect="AspectFit" x:Name="cantieriDown"></Image>
            <Image Grid.Column="2" Aspect="AspectFit" x:Name="flottaDown"></Image>
        </Grid>
    </Grid>
</ContentPage.Content>

我的列表视图:

ItemsSource="{Binding tmpPercentage}"

我的对象列表

public class tmpPercentange
{
    public string nickname { get; set; }
    public double percentage { get; set; }
    public int numeroInterventi { get; set; }
}

1 个答案:

答案 0 :(得分:0)

要绑定到源,它将类似于以下内容:

模型

public class TmpPercentange
{
    public string NickName { get; set; }

    public double Percentage { get; set; }

    public int NumeroInterventi { get; set; }
}

视图模型

public class MyPageViewModel
{
    /*
     * This does not have to be Observable. Any IEnumerable will work just fine.
     * We make this Observable so that any changes to the Collection will update
     * our View. It's also worth noting that any changes to a single item in our
     * Collection would not update in our View as our Model does not implement
     * `INotifyPropertyChanged`. 
     */
    public ObservableCollection Percentanges { get; set; }
}

视图

<ListView ItemsSource="{Binding Percentanges}">
    <ListView.ItemTemplate>
        <DataTemplate>
            <ViewCell>
                <StackLayout>
                    <Label Text="{Binding NickName}" />
                    <Label Text="{Binding Percentage}" />
                    <Label Text="{Binding NumeroInterventi}" />
                </StackLayout>
            </ViewCell>
        </DataTemplate>
    </ListView.ItemTemplate>
</ListView>