簡體   English   中英

如何在 xamarin ContentPage 中的 BindingContext 中使用多個值

[英]How to multiple value in BindingContext in xamarin ContentPage

我有 xamarin 應用程序,我需要在按鈕單擊事件上將兩個對象傳遞給 .cs 文件。

我有兩個 ListView 並在第二個 Listview 項目內有按鈕。 兩個 ListView 都將根據 JSON 動態加載。 現在的問題是我需要在按鈕單擊事件中發送父 ListView 數據源和第二個 ListView 數據源。 目前我只能使用 BindingContext 發送一個,但我需要將兩個或多個對象發送到 .cs 文件。

<ListView  x:Name="StaffListMaster_List" RowHeight="150"> 
<ListView.ItemTemplate>` 
<ListView x:Name="Staff_Record" ItemsSource="{Binding Path=detailsobj}">` 
<ListView.ItemTemplate>`
<DataTemplate>                                             
<ViewCell>  
<StackLayout  Orientation="Horizontal" >
<Button Clicked="OnActionSheetCancelDeleteClicked" BorderRadius="0"  BindingContext ="{Binding item}" Text="{Binding item[6]}"/>`

我想在 OnActionSheetCancelDeleteClicked(object sender, EventArgs e) {}` 中獲取 StaffListMaster_List 數據源和 Staff_Record 數據源

首先,不要那樣做。 這不是 BindingContexts 的用途,其次,如果您要響應后面代碼中的事件,則可以使用 ListView 的名稱訪問兩個數據源,即Staff_Record.ItemSource

如果您想在 Listview 中使用嵌套的 Listview,我們可以使用ListView.GroupHeaderTemplate來實現。

父項 class:

public class ParentItems : ObservableCollection<ChildItems>
{
    public string ID { get; set; }
    public string Title { get; set; }


    public ParentItems(List<ChildItems> list) : base(list)
    {

    }
}

兒童物品 class:

public class ChildItems
{
    public string ChildTitle { get; set; }
    public string Description { get; set; }
}

Xaml:

 <ListView HasUnevenRows="True" x:Name="listView" IsGroupingEnabled = "True">
            <ListView.GroupHeaderTemplate>
                <DataTemplate>
                    <ViewCell Height="40">
                        <StackLayout Orientation="Horizontal"
                             BackgroundColor="#3498DB"
                             VerticalOptions="FillAndExpand">
                            <Label Text="{Binding ID}"
                           VerticalOptions="Center" />
                            <Label Text="   "/>
                            <Label Text="{Binding Title}"
                           VerticalOptions="Center" />

                        </StackLayout>
                    </ViewCell>
                </DataTemplate>
            </ListView.GroupHeaderTemplate>
            <ListView.ItemTemplate>
                <DataTemplate>
                    <TextCell Text="{Binding ChildTitle}" Detail="{Binding Description}"></TextCell>
                </DataTemplate>
            </ListView.ItemTemplate>

        </ListView>

結果:

在此處輸入圖像描述

我已經上傳到 GitHub,您可以下載 ListViewBindng 文件夾以供參考 GitHub。 https://github.com/WendyZang/Test

最后我能夠解決如下問題:

Step 1: Create IMultiValueConverter interface Step 2: created MultiBinding.cs class (ref: [ https://gist.github.com/Keboo/0d6e42028ea9e4256715][1] ) Step 3: Include xmlns:multi="clr-namespace:MultiBindingExample " 命名空間步驟 4:

<Button   Command="{Binding ClearListItem}">
<Button.CommandParameter>  
<multi:MultiBinding   >     
    <Binding Source="{x:Reference control1}" Path="BindingContext"/>   
    <Binding Source="{x:Reference control2}" Path="BindingContext"/> 
 </Button.CommandParameter> 
 </Button>

第4步:在視圖model

 void ClearListViewSelectedItem( object param)
 {
    var commandParamList = (object[])param;
 }

最終的預期結果將是。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM