0
votes

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.

enter image description here

1
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 nowNikhileshwar

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.

  1. 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&amp;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&amp;g=1" />
    </DataTemplate>
</ContentPage.Resources>
  1. Initialize DynamicResource in Page.Xaml.cs
public MainPage()
{
    InitializeComponent();
    Resources["menuTemplate"] = Resources["menuTemplate1"];
}
  1. Bind the DynamicResource toItemTemplate`
<CollectionView ItemTemplate="{DynamicResource menuTemplate}" ItemsSource="{Binding ItemCollection}" />
  1. 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"];
    }
}

UI result

Please do check and comment for any query.