简体   繁体   English

从CodeBehind在DataTemplate内部绑定ListView ItemSource-Xamarin形式C#

[英]Binding listview ItemSource inside DataTemplate from codebehind - xamarin forms c#

I need to bind the listview with the data. 我需要将listview与数据绑定。 the problem is that this listview is inside a DataTemplate, and I can not call it from codebehind. 问题是此列表视图位于DataTemplate内部,而我无法从代码隐藏中调用它。 In WPF I would have drawn the object from a sender, but with Xamarin forms will not work. 在WPF中,我应该从发件人处提取对象,但使用Xamarin表单将无法使用。 I tried to use an ObservableCollection but should not be the same, someone has suggestions? 我尝试使用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>

my listview: 我的列表视图:

ItemsSource="{Binding tmpPercentage}"

my object list 我的对象清单

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

To bind to a source it would look something like the following: 要绑定到源,它看起来类似于以下内容:

Model 模型

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

    public double Percentage { get; set; }

    public int NumeroInterventi { get; set; }
}

ViewModel 视图模型

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; }
}

View 视图

<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>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 使用Xamarin Forms从DataTemplate内部的代码背后调用变量 - call variable from codebehind inside DataTemplate with Xamarin Forms Xamarin.Forms:从代码隐藏项将项目添加到ListView而不进行绑定? - Xamarin.Forms: Add items to ListView from codebehind without binding? 在Xamarin表单背后的代码中设置ListView ItemSource绑定 - Set ListView ItemSource Binding in Code Behind Xamarin Forms 列表视图结束; xaml=&gt;ListView=&gt;Scrolled new Event=&gt;CodeBehind ListView_Scrolled // c# xamarin forms - 用于 ZADD4E130E8FD270C - End of ListView; xaml=>ListView=>Scrolled new Event=>CodeBehind ListView_Scrolled // c# xamarin forms - for UWP 如何在Xamarin.Forms中访问listview datatemplate内的按钮? - How to access button click inside listview datatemplate in Xamarin.Forms? 命令绑定在Xamarin Forms中的Listview内不起作用 - Command binding not working inside Listview in Xamarin Forms Xamarin表单按钮命令绑定在ListView中 - Xamarin Forms Button Command binding inside a ListView Xamarin表单用户控件绑定到ListView中 - Xamarin Forms User Control Binding inside ListView ListView 中的 Xamarin Forms 命令绑定不起作用 - Xamarin Forms Command Binding inside ListView is not working ObservableCollection绑定异常-ListView ItemSource Xamarin - ObservableCollection Binding Exception - ListView ItemSource Xamarin
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM