如何从项模板内部访问页面视图模型的属性?



我有一个XAML。列表EDocuments有EDocument模型。列出主视图模型的EDocuments部分。当我读取这是数据,我需要隐藏一些细节,为示例一小段代码:

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

但是字段NewDocumentsExists这是主ViewModel的一部分,而不是EDocument。我怎样才能使用它?

主:

<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>  
<<p>字段strong> NewDocumentsExists 在主视图模型中,与列表EDocuments没有关系. 如何访问newdocumentsexsts? 我试过那个ViewModel。newdocumentsexsts - not is work.

您只需要设置绑定的源。在您的XAML中,在xmlns语句的顶部,为您的页面命名为x:Name="MyAwesomePage"。然后你可以在IsVisible属性中引用它:

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

最新更新