I'm study Xamarin, I have Xamarin Forms using Collection View. I want to create master menu on home page in android. I can't auto change show view item in Collection View.
0
votes
What does the image say? Do you want the collection view to change it's item's view dynamically? Or do you want a page navigation?
– Nikhileshwar
hi @Nikhileshwar. I want the collection view to change it's item's view dynamically but I don't know how to do it. Can you help me?
– Thong Pham
Can you add code for what you have tried as of now
– Nikhileshwar
1 Answers
0
votes
Bind the ItemTemplate to a DynamicResource
and change the Resource
on a button click or where you want the change to happen.
- Define the templates in resource of the page.
<ContentPage.Resources>
<DataTemplate x:Key="menuTemplate1">
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="0.3*" />
<ColumnDefinition Width="0.7*" />
</Grid.ColumnDefinitions>
<Image Source="https://i.stack.imgur.com/di65V.jpg?s=328&g=1" />
<StackLayout Grid.Column="2">
<Label Text="Hi there" />
<Label Text="Hi there" />
</StackLayout>
</Grid>
</DataTemplate>
<DataTemplate x:Key="menuTemplate2">
<Image Source="https://i.stack.imgur.com/di65V.jpg?s=328&g=1" />
</DataTemplate>
</ContentPage.Resources>
- Initialize
DynamicResource
in Page.Xaml.cs
public MainPage()
{
InitializeComponent();
Resources["menuTemplate"] = Resources["menuTemplate1"];
}
- Bind the
DynamicResource to
ItemTemplate`
<CollectionView ItemTemplate="{DynamicResource menuTemplate}" ItemsSource="{Binding ItemCollection}" />
- Change the
DynamicResource
on button click or where you want.
void Button_Clicked(System.Object sender, System.EventArgs e)
{
if(Resources["menuTemplate"] == Resources["menuTemplate1"])
{
Resources["menuTemplate"] = Resources["menuTemplate2"];
}
else
{
Resources["menuTemplate"] = Resources["menuTemplate1"];
}
}
Please do check and comment for any query.