My ViewModel needs to know which AccordionItem is selected in the View. So in the ViewModel I have:
public class ServerListControlViewModel : ObservableObject
{
private int _accordion_index;
public int accordion_index
{
get { return _accordion_index; }
set
{
_accordion_index = value;
RaisePropertyChanged("accordion_index");
}
}
}
And in the View, I have
<toolkitLayout:Accordion SelectedIndex="{Binding accordion_index}">
<toolkitLayout:AccordionItem items go here>
</toolkitLayout:Accordion>
The problem? The Accordion does the fancy expand/collapse animation, but accordion_index's set method is never getting called. Furthermore, I can set accordion_index to something in the VM's constructor, and the get method will be called when the View is loaded, and my hardcoded value will be returned, but the Accorion ignores it and always defaults to the first item being expanded. Why? I am new to WPF but fairly certain I've bound this correctly.
As vorrtext pointed out, you need to add the Mode=TwoWay
to your binding. Accordion derives from ItemsControl and adds it's own SelectedIndex property.
With the ListBox, or any control that derives from Selector, the SelectedIndex will bind two-way by default. They do that because they specify the FrameworkPropertyMetadataOptions.BindsTwoWayByDefault in the property metadata. The Accordion does not do this, so you have to do it manually every time.