Forum Xamarin.Forms

Raising property changed from Custom control to update the model

ktunnelktunnel USMember ✭✭
edited June 2017 in Xamarin.Forms

I have a custom control for an accordion. I am putting this in a listview and am binding the IsExpanded property on the custom control. I have a tapped event on the control that will reverse the IsExpanded property. The accordion works fine. But the IsExpanded property on the model in the view model is not being updated. if the listview item is expanded, I want change the visibility of a label. But my property on the model is not being updated. what am I missing?

`

            <ListView HasUnevenRows="True" ItemsSource="{Binding Faqs}" SeparatorVisibility="None">
    <ListView.ItemTemplate>
                <DataTemplate>
                    <ViewCell>
                        <controls:ListAccordionItem IsExpanded="{Binding IsExpanded}">
                            <controls:ListAccordionItem.Header>
                                <Frame Padding="10" BackgroundColor="Gray" OutlineColor="Gray">
                                    <StackLayout>
                                        <Label></Label>
                                        <Label Text="{Binding Question}"></Label>
                                        <Label></Label>
                                        <Label Text="View Detail" IsVisible="{Binding IsCollapsed}"></Label>
                                        <Label Text="HideDetail" IsVisible="{Binding IsExpanded}"></Label>
                                    </StackLayout>
                                </Frame>
                            </controls:ListAccordionItem.Header>
                            <controls:ListAccordionItem.Content>
                                <StackLayout Padding="10">
                                    <Label></Label>
                                    <Label Text="{Binding Answer}"></Label>
                                    <Label></Label>
                                </StackLayout>
                            </controls:ListAccordionItem.Content>
                        </controls:ListAccordionItem>
                    </ViewCell>
                </DataTemplate>
            </ListView.ItemTemplate>
        </ListView>

`

My Model:
`

public class FAQ : BindableBase{
    public string Question { get; set; }
    public string Answer { get; set; }
    private bool _isExpanded;
    public bool IsExpanded { get { return _isExpanded; }
        set { SetProperty(ref _isExpanded, value);
            base.OnPropertyChanged();
            IsCollapsed = !_isExpanded;
        }
    }
    private bool _isCollapsed;
    public bool IsCollapsed
    {
        get { return _isCollapsed; }
        set
        {
            SetProperty(ref _isCollapsed, value);
            base.OnPropertyChanged();
        }
    }
}

`

Sign In or Register to comment.