This should (hopefully) be an easy thing to answer. I have a collection view that is bound to a JSON data source, and a button in the first level of the JSON file that I store a value for (in Preferences) and change the button based on if it’s stored or not (color, text changes). It then creates a child collection view, which each of the items also have a button for a similar function, except that if the first button is clicked, I want to be able to hide the buttons that are for the children.
Think of it as this: you have a list of box of different types of unique widgets. The parent button allows you to click to save that you own all of the widgets without having to click on each one, but if you haven’t clicked the full box, and you only own Widget A, I want to allow you to select Widget A and have it track that you own only Widget A. So, to accomplish scenario one, I want to have a user click the parent button, which would then make all of the buttons in the child area for that parent not visible so that it won’t get confusing having the child buttons there if the parent is marked as owned.
I have the collection views set up, the buttons created, and function that saves whether one or the other is owned, and it all works great. I just can’t find any examples that aren’t extremely complicated with code that obfucsates what to do on a conceptual level. I thought about doing an observable object, but not sure how to accomplish that when there are multiple parent objects. I also found something about ancestor binding, but I couldn’t find an example that explained it all very well. One of my coworkers suggested looping through the child controls, but I haven’t found a good example of that, and not sure how well that would work.
Here is a (very) trimmed down example of the XAML file:
<CollectionView x.Name="ParentCollection" ItemsSource="{Binding Parent}">
<CollectionView.ItemTemplate>
<DataTemplate>
<Frame>
<Button Text="{Binding ParentOwned}" Clicked="OwnedButtonClick" WidthRequest="100"></Button>
<ScrollView>
<CollectionView x.Name="ChildCollection" ItemsSource="{Binding Child}>
<CollectionView.ItemTemplate>
<DataTemplate>
<Frame>
<Button Text="{Binding ChildOwned}" Clicked="OwnedButtonClick" WidthRequest="100"></Button>
</Frame>
</DataTemplate>
</CollectionView.ItemTemplate>
</CollectionView>
</ScrollView>
</Frame>
</DataTemplate>
</CollectionView.ItemTemplate>
</CollectionView>
Of course, it’s FAR more complicated than this, but I want to keep the question and solution as simple as possible for clarity sake (and for those who may come in behind me with the same question).
Haven’t really tried much at the moment, as I haven’t found a good direction to go with it yet.
Joe Schmidt is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.