-2

I have an Expander in my View that every time I touch it it opens and displays data. Is it possible to open the Expander or close it from ViewModel? What I want to do is that the Expander can open or close it by pressing a Button.

MainPage.xaml:

    <StackLayout>
        <StackLayout>
            <CollectionView>
                <CollectionView.ItemTemplate>
                    <DataTemplate>
                        <ContentView>
                            <Frame>
                                <StackLayout>
                                    <Expander x:Name="expander"></Expander>
                                </StackLayout>
                            </Frame>
                        </ContentView>
                    </DataTemplate>
                </CollectionView.ItemTemplate>
            </CollectionView>
        </StackLayout>
        <StackLayout>
            <ImageButton Source="img1" Command="{Binding xxxCommand}" CommandParameter="{Binding Source={x:Reference expander}, Path=.}"/>
        </StackLayout>
    </StackLayout>

ViewModel:

    xxxCommand = new Command((sender)=> {

     var exp = sender as Expander;
     exp.IsExpanded = !exp.IsExpanded;
     //...
            
});

When I open the app, I get this exception:

Xamarin.Forms.Xaml.XamlParseException: Can not find the object referenced by expander.

Cfun
  • 8,442
  • 4
  • 30
  • 62
LololYut
  • 65
  • 7

1 Answers1

0

You could pass the Expander as parameter to the Command of the button .

Set the name of Expander

 <Expander x:Name="exp">
 <Button Text="XXX"  Command="{Binding xxxCommand}" CommandParameter="{Binding Source={x:Reference exp}, Path=.}" />

In ViewModel

xxxCommand = new Command((sender)=> {

     var exp = sender as Expander;
     exp.IsExpanded = !exp.IsExpanded;
     //...
            
});

Update

In your case you could use Data binding

<StackLayout>
      <Expander  IsExpanded="{Binding IsExpanded}"></Expander>
</StackLayout>
<ImageButton Source="img1" Command="{Binding xxxCommand}"/>

Add a new property in your model

public class YourModel : INotifyPropertyChanged
{

    public event PropertyChangedEventHandler PropertyChanged;

    public void OnPropertyChanged(string propertyname)
    {
        PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyname));
    }

    public bool isExpanded;
    public bool IsExpanded
    {
        get { return isExpanded; }
        set
        {
            isExpanded= value;
            OnPropertyChanged("IsExpanded");
        }
    }


    //...other properties

}

In ViewModel

//list here is the ItemsSource of your listview
ObservableCollection<YourModel> list = new ObservableCollection<YourModel>();

//list.Add();
//list.Add();
//list.Add();
//list.Add();
Command xxxCommand = new Command(()=> {

   //if you want to open the first Expander  

   list[0].IsExpanded = true;


});
Lucas Zhang
  • 18,630
  • 3
  • 12
  • 22
  • I have a problem with this. My application crashes because my Expander is inside a CollectionView, a Frame and StackLayout, and when putting its x: Name in the CommandParameter it tells me that it does not find anything called expander. – LololYut Jan 28 '21 at 11:08
  • The Expander control is known to show unwanted behavior when used in a ListView or CollectionView. At this time we recommend not using a Expander in one of these controls.Check https://learn.microsoft.com/en-us/xamarin/community-toolkit/views/expander – Lucas Zhang Jan 28 '21 at 11:14
  • Check my update answer . Don't forget to accept it if it helps you :) – Lucas Zhang Jan 28 '21 at 11:32
  • Can't use list[0].IsExpanded = true; , I only can use IsExpanded = true; alone and it don't work, it is called but nothing change in the view. – LololYut Jan 28 '21 at 11:50
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/227956/discussion-between-lucas-zhang-msft-and-lololyut). – Lucas Zhang Jan 28 '21 at 11:53