【问题标题】:How to multiple value in BindingContext in xamarin ContentPage如何在 xamarin ContentPage 的 BindingContext 中设置多个值
【发布时间】:2020-01-26 14:40:43
【问题描述】:

我有 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]}"/>`

我想在里面获取 StaffListMaster_List 数据源和 Staff_Record 数据源 OnActionSheetCancelDeleteClicked(object sender, EventArgs e) {}`

【问题讨论】:

    标签: c# xaml xamarin


    【解决方案1】:

    首先,不要那样做。这不是 BindingContexts 的用途,其次,如果您在后面的代码中响应事件,则可以使用 ListView 的名称访问两个数据源,即 Staff_Record.ItemSource

    【讨论】:

      【解决方案2】:

      如果您想在 Listview 中使用嵌套的 Listview,我们可以使用 ListView.GroupHeaderTemplate

      ParentItems 类:

      public class ParentItems : ObservableCollection<ChildItems>
      {
          public string ID { get; set; }
          public string Title { get; set; }
      
      
          public ParentItems(List<ChildItems> list) : base(list)
          {
      
          }
      }
      

      ChilsItems 类:

      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,你可以在 GitHub 上下载 ListViewBindng 文件夹供参考。 https://github.com/WendyZang/Test

      【讨论】:

        【解决方案3】:

        我终于可以解决这个问题了:

        第 1 步:创建 IMultiValueConverter 接口 第 2 步:创建 MultiBinding.cs 类(参考:[https://gist.github.com/Keboo/0d6e42028ea9e4256715][1]) 第 3 步:包含 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 步:在视图模型中

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

        最终的预期结果是。

        【讨论】:

          猜你喜欢
          • 2016-10-18
          • 2021-12-04
          • 2016-12-26
          • 2018-10-22
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2016-12-08
          • 2015-02-09
          相关资源
          最近更新 更多