I am new to xamarin so still learning all its functionalities and limits. I am curious to know if you can redirect from the selection of a listview to another page containing a listview based on the foreign key. In this example the foreign key is CompanyID. Thanks!
Models:
public class Company
{
public int ID { get; set; }
public string Name { get; set; }
}
public class WellGroup
{
public int ID { get; set; }
public string Name { get; set; }
public int CompanyID { get; set; }
}
xaml:
<ListView x:Name="CompaniesCollection"
ItemsSource="{Binding CompaniesCollection}"
SelectionMode="Single"
HasUnevenRows="True"
ItemSelected="Companies_Selection">
<ListView.ItemTemplate>
<DataTemplate>
<ViewCell>
<StackLayout Padding="10" x:DataType="model:Company">
<Label Text="{Binding Name}"
FontSize="Large"
VerticalOptions="Center"/>
</StackLayout>
</ViewCell>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
CodePudding user response:
You could pass the index to show the Company ID and Name according to the specific CompanyID.
Page2: Show the CompanyID
Xaml:
<ListView x:Name="CompaniesCollection"
ItemsSource="{Binding CompaniesCollection}"
SelectionMode="Single"
HasUnevenRows="True"
ItemSelected="Companies_Selection">
<ListView.ItemTemplate>
<DataTemplate>
<ViewCell>
<StackLayout Padding="10" x:DataType="model:WellGroup">
<Label Text="{Binding CompanyID}"
FontSize="Large"
VerticalOptions="Center"/>
</StackLayout>
</ViewCell>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
Code behind:
public partial class Page2 : ContentPage
{
public Page2()
{
InitializeComponent();
this.BindingContext = new WellGroupViewModel();
}
private async void Companies_Selection(object sender, SelectedItemChangedEventArgs e)
{
var index = e.SelectedItemIndex;
await Navigation.PushAsync(new Page3(index));
}
}
public class Company
{
public int ID { get; set; }
public string Name { get; set; }
}
public class WellGroup
{
public List<Company> Companies { get; set; }
public int CompanyID { get; set; }
}
public class WellGroupViewModel
{
public List<WellGroup> CompaniesCollection { get; set; }
public WellGroupViewModel()
{
var C1 = new WellGroup();
C1.Companies = new List<Company>() {
new Company(){ ID=1, Name="A"},
new Company(){ ID=1, Name="B"},
new Company(){ ID=1, Name="C"},
new Company(){ ID=1, Name="D"}
};
C1.CompanyID = 111;
var C2 = new WellGroup();
C2.Companies = new List<Company>()
{
new Company(){ ID=2, Name="A"},
new Company(){ ID=2, Name="B"},
new Company(){ ID=2, Name="C"},
new Company(){ ID=2, Name="D"}
} ;
C2.CompanyID = 222;
var C3 = new WellGroup();
C3.Companies = new List<Company>()
{
new Company(){ ID=3, Name="A"},
new Company(){ ID=3, Name="B"},
new Company(){ ID=3, Name="C"},
new Company(){ ID=3, Name="D"}
};
C3.CompanyID = 333;
CompaniesCollection = new List<WellGroup> { C1, C2, C3 };
}
}
Page3: Show the ID, Name according to the CompanyID
Xaml:
<ListView x:Name="CompaniesCollection"
ItemsSource="{Binding List}"
SelectionMode="Single"
HasUnevenRows="True">
<ListView.ItemTemplate>
<DataTemplate>
<ViewCell>
<StackLayout Padding="10" Orientation="Horizontal" >
<Label Text="{Binding ID}"
VerticalOptions="Center"/>
<Label Text="{Binding Name}"
VerticalOptions="Center"/>
</StackLayout>
</ViewCell>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
Code behind:
public partial class Page3 : ContentPage
{
private List<Company> _companies;
public List<Company> List
{
get { return _companies; }
set
{
_companies = value;
OnPropertyChanged("List");
}
}
public Page3(int index)
{
InitializeComponent();
List = new List<Company>();
List = ((new WellGroupViewModel()).CompaniesCollection[index]).Companies;
this.BindingContext = this;
}
}