Xamarin 表单从列表视图绑定到视图模型
Xamarin forms binding to viewmodel from listview
我有一个从我的 viewModel 绑定到 bindingList 的列表视图,所以我可以使用模型中的 属性 名称,但是我如何再次绑定到 viewmodel 中的 属性在我的列表视图中?
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
x:Class="Finance.Pages.ItemsPage"
xmlns:conv="clr-namespace:Finance.Converters"
x:Name="Page">
<ContentPage.Resources>
<ResourceDictionary>
<conv:ItemTypeNameToStringConverter x:Key="ItemTypeNameToStringConverter" />
<conv:ColumnToColorConverter x:Key="ColumnToColorConverter" />
</ResourceDictionary>
</ContentPage.Resources>
<ContentPage.Content>
<StackLayout Padding="10,10">
<StackLayout Orientation="Horizontal">
<Label Text="Compound Items:"></Label>
<CheckBox IsChecked="{Binding CompoundItems}"></CheckBox>
</StackLayout>
<ListView ItemsSource="{Binding Items}">
<ListView.ItemTemplate>
<DataTemplate>
<ViewCell>
<ViewCell.ContextActions>
<MenuItem Text="Edit" Command="{Binding Path=EditItemCommand}" CommandParameter="{Binding .}"/>
<MenuItem Text="Delete" Command="{Binding DeleteItemCommand}" CommandParameter="{Binding .}"/>
</ViewCell.ContextActions>
<StackLayout Orientation="Horizontal" >
<Label Text="{Binding Name, Converter={StaticResource ItemTypeNameToStringConverter}}" HorizontalOptions="StartAndExpand"/>
<Label Text="{Binding Count}" BindingContext="{x:Reference Name=Page}" IsVisible="{Binding CompoundItems}" HorizontalOptions="StartAndExpand"/>
<Label Text="{Binding Amount}" TextColor="{Binding Destination, Converter={StaticResource ColumnToColorConverter}}"></Label>
</StackLayout>
</ViewCell>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
</StackLayout>
</ContentPage.Content>
</ContentPage>
属性 CompoundItems 是我的视图模型中的布尔值。
提前致谢,
您可以:
IsVisible="{Binding BindingContext.CompoundItems, Source={x:Reference Name=Page}}"
或者使用 relativesource 获取父 BindingContext
<Label Text="{Binding Count}" IsVisible="{Binding CompoundItems, Source={RelativeSource AncestorType={x:Type local:YourViewModelType}}}"/>
我有一个从我的 viewModel 绑定到 bindingList 的列表视图,所以我可以使用模型中的 属性 名称,但是我如何再次绑定到 viewmodel 中的 属性在我的列表视图中?
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
x:Class="Finance.Pages.ItemsPage"
xmlns:conv="clr-namespace:Finance.Converters"
x:Name="Page">
<ContentPage.Resources>
<ResourceDictionary>
<conv:ItemTypeNameToStringConverter x:Key="ItemTypeNameToStringConverter" />
<conv:ColumnToColorConverter x:Key="ColumnToColorConverter" />
</ResourceDictionary>
</ContentPage.Resources>
<ContentPage.Content>
<StackLayout Padding="10,10">
<StackLayout Orientation="Horizontal">
<Label Text="Compound Items:"></Label>
<CheckBox IsChecked="{Binding CompoundItems}"></CheckBox>
</StackLayout>
<ListView ItemsSource="{Binding Items}">
<ListView.ItemTemplate>
<DataTemplate>
<ViewCell>
<ViewCell.ContextActions>
<MenuItem Text="Edit" Command="{Binding Path=EditItemCommand}" CommandParameter="{Binding .}"/>
<MenuItem Text="Delete" Command="{Binding DeleteItemCommand}" CommandParameter="{Binding .}"/>
</ViewCell.ContextActions>
<StackLayout Orientation="Horizontal" >
<Label Text="{Binding Name, Converter={StaticResource ItemTypeNameToStringConverter}}" HorizontalOptions="StartAndExpand"/>
<Label Text="{Binding Count}" BindingContext="{x:Reference Name=Page}" IsVisible="{Binding CompoundItems}" HorizontalOptions="StartAndExpand"/>
<Label Text="{Binding Amount}" TextColor="{Binding Destination, Converter={StaticResource ColumnToColorConverter}}"></Label>
</StackLayout>
</ViewCell>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
</StackLayout>
</ContentPage.Content>
</ContentPage>
属性 CompoundItems 是我的视图模型中的布尔值。
提前致谢,
您可以:
IsVisible="{Binding BindingContext.CompoundItems, Source={x:Reference Name=Page}}"
或者使用 relativesource 获取父 BindingContext
<Label Text="{Binding Count}" IsVisible="{Binding CompoundItems, Source={RelativeSource AncestorType={x:Type local:YourViewModelType}}}"/>