问题
I have xamarin application where I need to pass two objects to .cs file on button click event.
I have two ListView and have button in inside the 2nd Listview items. Both ListViews will be loaded dynamically based on the JSON. Now the problem is I need to send Parent ListView datasource and second ListView data source in the button click event. currently I am able to send only one using BindingContext but I need to send two or more objects to .cs file.
<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]}"/>`
I want to get StaffListMaster_List data source and Staff_Record datasource inside OnActionSheetCancelDeleteClicked(object sender, EventArgs e) {}`
回答1:
First of all, don't do that. that's not what BindingContexts are for, secondly if you are responding to the event on your code behind you could just access both the DataSources using the ListView's name i.e. Staff_Record.ItemSource
回答2:
If you want to use a nested Listview inside Listview, we could do that with ListView.GroupHeaderTemplate
.
ParentItems class:
public class ParentItems : ObservableCollection<ChildItems>
{
public string ID { get; set; }
public string Title { get; set; }
public ParentItems(List<ChildItems> list) : base(list)
{
}
}
ChilsItems 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>
Result:
I have uploaded on GitHub, you could download ListViewBindng folder for reference on GitHub. https://github.com/WendyZang/Test
回答3:
Finally I am able to solve the issue as follows :
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" namespace step 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>
step 4 : In view model
void ClearListViewSelectedItem( object param)
{
var commandParamList = (object[])param;
}
Final expected result would be.
来源:https://stackoverflow.com/questions/58114752/how-to-multiple-value-in-bindingcontext-in-xamarin-contentpage