How access property of page's ViewModel, from inside an ItemTemplate?

Viewed 65

I have next XAML. List EDocuments have model EDocument. List EDocuments part of main ViewModel. When I read this is data, I need hide some details, for example little part of code:

StackLayout Orientation="Horizontal" Padding="15, 15, 0, 0" IsVisible="{Binding NewDocumentsExists}"  

But field NewDocumentsExists this is par of main ViewModel, not EDocument. How Can I get access to it?

Main:

<StackLayout  BindableLayout.ItemsSource="{Binding EDocuments}">
    <BindableLayout.ItemTemplate>
        <DataTemplate>
            <Frame CornerRadius="5"  OutlineColor="#c3c3c3" BackgroundColor="#F2FFFFE0">
                <StackLayout Orientation="Vertical" HorizontalOptions="FillAndExpand">
                    <StackLayout Orientation="Horizontal">
                        <Label Style="{StaticResource LabelTitleFontSize}" Text="Номер документа:" ></Label>
                        <Label Style="{StaticResource LabelFontSize}" Text="{Binding DocumentNum}"></Label>
                    </StackLayout>
                    <StackLayout Orientation="Horizontal">
                        <Label Style="{StaticResource LabelTitleFontSize}" Text="Точка вивантаження:" ></Label>
                        <Label Style="{StaticResource LabelFontSize}" Text="{Binding UnloadPoint}"></Label>
                    </StackLayout>
                    <StackLayout Orientation="Horizontal">
                        <Label Style="{StaticResource LabelTitleFontSize}" Text="Кількість місць:" ></Label>
                        <Label Style="{StaticResource LabelFontSize}" Text="{Binding PlacesTotal}"></Label>
                    </StackLayout>
                    <StackLayout Orientation="Horizontal">
                        <Label Style="{StaticResource LabelTitleFontSize}" Text="Загальна вага:" ></Label>
                        <Label Style="{StaticResource LabelFontSize}" Text="{Binding WeightTotal}"></Label>
                    </StackLayout>
                    <StackLayout Orientation="Horizontal">
                        <Label Style="{StaticResource LabelTitleFontSize}" Text="Статус:" ></Label>
                        <Label Style="{StaticResource LabelTitleFontSize}" TextColor="Red" Text="{Binding EDocState.StateName}"></Label>
                    </StackLayout>
                    <StackLayout Orientation="Horizontal">
                        <Line BackgroundColor="Black" HeightRequest="1" />
                        <Label Text="Перелік товару:" TextColor="Black" FontSize="22"/>
                    </StackLayout>
                    <StackLayout  BindableLayout.ItemsSource="{Binding Specifications}">
                        <BindableLayout.ItemTemplate>
                            <DataTemplate>
                                <Frame CornerRadius="5" OutlineColor="#c3c3c3" BackgroundColor="White">
                                    <StackLayout>
                                        <StackLayout Orientation="Horizontal" HorizontalOptions="FillAndExpand">
                                            <Label Style="{StaticResource LabelTitleFontSize}" Text="Найменування:"/>
                                            <Label Style="{StaticResource LabelFontSize}" Text="{Binding Name}"/>
                                        </StackLayout>
                                
                                        <StackLayout Orientation="Horizontal" HorizontalOptions="FillAndExpand">
                                            <Label Style="{StaticResource LabelTitleFontSize}" Text="Тип:"/>
                                            <Label Style="{StaticResource LabelFontSize}" Text="{Binding TypeOfPackaging}"/>
                                        </StackLayout>

                                        <StackLayout Orientation="Horizontal" HorizontalOptions="FillAndExpand">
                                            <Label Style="{StaticResource LabelTitleFontSize}" Text="Вага:"/>
                                            <Label Style="{StaticResource LabelFontSize}" Text="{Binding GrossWeight}"/>
                                        </StackLayout>

                                        <StackLayout Orientation="Horizontal" HorizontalOptions="FillAndExpand">
                                            <Label Style="{StaticResource LabelTitleFontSize}" Text="Кількість місць:"/>
                                            <Label Style="{StaticResource LabelFontSize}" Text="{Binding NumberOfSeats}"/>
                                        </StackLayout>
                                    </StackLayout>
                                </Frame>
                            </DataTemplate>
                        </BindableLayout.ItemTemplate>
                    </StackLayout>
                    <StackLayout Orientation="Horizontal" Padding="15, 15, 0, 0" IsVisible="{Binding **NewDocumentsExists**}" IsEnabled="{Binding NewDocumentsExists}">
                        <Label Style="{StaticResource LabelTitleFontSize}" Text="Вибрати:" Margin="0, 0, 20, 0" />
                        <Switch Scale="1.5" IsToggled="{Binding NewDocumentsExists}"  OnColor="LightGray" ThumbColor="{Binding SwitchColor}" />
                    </StackLayout>
                </StackLayout>
            </Frame>
        </DataTemplate>
    </BindableLayout.ItemTemplate>
</StackLayout>  

Field NewDocumentsExists in main viewmodel, and not have relationship to list EDocuments. How can I get access to NewDocumentsExists? I tried that ViewModel.NewDocumentsExists - not is work.

1 Answers

You just need to set the source of the binding. In your XAML, at the top with the xmlns statements, give your page a name like x:Name="MyAwesomePage". Then you can reference it in the IsVisible Property:

IsVisible="{Binding BindingContext.NewDocumentExists, Source={x:Reference MyAwesomePage}}"
Related