I have ListView which has a ListView.Header. The ListView is bound to an ObservableCollection. The Header needs to bind to members of my view model, not the collection. I cant figure out the correct syntax:
view model
public class MilitaryAssestsDetailsViewModel
{
public int Armies
{
get { return GetArmies(); }
}
public ObservableCollection<TerritoryModel> TerritoryList
{
get { return _gameData.PlayerLand.Territories; }
}
}
xaml:
<StackLayout>
<ListView ItemsSource="{Binding TerritoryList}" >
<ListView.Header>
<StackLayout>
<Label Text="Your Military:" />
<StackLayout Orientation="Horizontal">
<Label Text="{Binding Path=Armies, StringFormat='{0} Armies'}}" />
</StackLayout>
</StackLayout>
</ListView.Header>
<ListView.ItemTemplate>
</ListView.ItemTemplate>
</ListView>
</StackLayout>
The binding to "TerritoryList" works as a list appears. But the content for the header does not work.
I've tried:
<Label Text="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type StackLayout}}, Path=Armies}" />
but it throws an exception that RelativeSource markup extension doesn't exist.
Thnx
Matt