opened
DavidShochet
username_0
I am trying to use the accordion, and I see that IsOpen property is not bindable. It is very important to be able to bind it to a viewmodel property. Am I missing something?
IsOpen not bindable
issue
created
DottorPagliaccius
username_1
IsOpen is not bindable because I don't think it should be. Change my mind! ;)

Seriously, why it's so important?
comment
created
DavidShochet
username_0
In my project, I have several collapsible panels, and the user enters data into one ore more of their controls. Then the user presses the "Calculate" button, and calculations are performed. Only the data from open panel(s) is taken into account. But I don't see how I can determine from the view model what is open and what is closed if IsOpen is not bindable. Could you please suggest an alternative approach?
comment
created
DottorPagliaccius
username_1
Uhm I see your point, but I still don't know if it's right to make that property data-bindable... 

I'll think about it, in the meanwhile you can trace the state using OnClick event (I know it's not so elegant, I should have added a ClickCommand for MVVM), so you can trace the state of all panels using a class variable, something like a `List<AccordionItemView>` so you can iterate through them. Or you can extracting the bound item in the event handler and set manually the boolean property you were intended to bind to IsOpen property (the better temporary solution I think).

Something like

```
 public void Click_EventHandler(object sender, AccordionItemClickEventArgs e)
 {
     var item = (YourObject)e.Item.BindingContext;
     item.IsOpen = e.Item.IsOpen; 
 }
```
to register the handler, you can override the  OnChildAdded event of the main control:

```
protected override void OnChildAdded(Element child)
{
      base.OnChildAdded(child);
      var accordionItemView = child as AccordionItemView;     
      accordionItemView.OnClick += Click_EventHandler;
}
```
comment
created
DavidShochet
username_0
Thank you for your answer. It would be a good idea... but I don't use code behind, but rather FreshMvvm... I don't see how I can do it...
comment
created
DottorPagliaccius
username_1
If you have a page you must have a codebehind file.

Nonetheless, I just read my answer and I was very innacurate; I will update project soon but I can't tell you when, meanwhile the only thing you can do is **subclass** AccordionViewItem and add

```
protected override void OnChildAdded(Element child)
{
      base.OnChildAdded(child);
      var accordionItemView = child as AccordionItemView;     
      accordionItemView.OnClick += Click_EventHandler;
}

private void Click_EventHandler(object sender, AccordionItemClickEventArgs e)
{
     var item = (YourObject)e.Item.BindingContext;
     item.IsOpen = e.Item.IsOpen; 
}
```
So you don't have to write in codebehind at all. Very crappy workaround, but it should work.  


For the "why is not-bindable-question", it's a tricky one, I think that Accordion doesn't have to track the open/closed status of its child controls in the bound data, so, if you have to, maybe you're using it in the wrong way or for the wrong purpose. But maybe this is not the case, you have to understand that I cannot blindly say "yeah, [let's do it](https://www.youtube.com/watch?v=7qf_QorYgDE)!" to every features users may ask me, I have to think about it. But I see your point, so I think I will do something about in the next version.
comment
created
DavidShochet
username_0
Thank you, I will wait for the new version.
comment
created
DavidShochet
username_0
Hi, I have questions on the code you provided. 
1) Do I understand correctly that you meant inheriting from AccordionView, not AccordionItemView?
2) I don't understand how Click_EventHandler() can help me set the values of my boolean properties, e.g. PanelOneOpened, PanelTwoOpened etc.

Thanks.
comment
created
DottorPagliaccius
username_1
Hi

1) Yeah
2) I suppose the object bound has a "IsOpen" property or something like that, the property you want to be bindable in xaml (e.g. IsOpen={Binding IsOpen}).

In the click event e.Item.BindingContext is the element of your list that's been clicked, so you can set manually the value. You don't have to set manually a series of PanelNOpen (Open is more correct than Opened being an adjective). 

If you subclass AccordionView and simply add the code I provided (replacing _YourObject_ with your objet type, of course) it should work.
comment
created
DavidShochet
username_0
You see, I use your panels not as templates in a ListView that would hold similar data, but as containers for sections of the View, which are very different. That is why each needs to bind to a separate boolean property of the ViewModel. 

Thank you for correcting me on grammar :)
comment
created
DottorPagliaccius
username_1
ahah I'm not really able to teach english grammar, last time I checked I was still italian, but I try ;) 

Ok, now it's more clear. So you have a situation like [this](https://github.com/username_1/Xamarin-Custom-Controls/blob/master/src/CustomControlsSamples/AccordionViewPage.xaml), isn't it?
comment
created
DavidShochet
username_0
Yes, that is it.
comment