如何绑定到视图模型而不是页面 [英] How to bind to a viewmodel instead of a page
问题描述
我的问题是viewcell,因为它是IssueModel类,所以找不到OnDelete命令,我试图更改Listview的绑定上下文,但是除了上述绑定之外,它没有任何改变.
My problem is with the viewcell, the OnDelete command is not found due to it being of the IssueModel class, I've attempted to change the binding-context of the Listview, but that doesn't change anything except the above binding.
是否有任何方法可以更改视单元的绑定上下文,所以我不必将命令放入IssueModel中?我尝试了以下解决方案,但出现错误
Is there any way to change the binding context of the viewcell so I don't have to put the command into to IssueModel? I've attempted the below solution but I get an error
无效的标记扩展名:预期类型为对象,实际类型为Issuepagemodel"
"Invalid markup extension: expected type is object, actual type is Issuepagemodel"
xmlns:pageModels="clr-namespace:ASFT.PageModels;assembly=ASFT"
<ListView ItemsSource="{Binding Issues}" SeparatorColor="#444444" RowHeight="90" IsPullToRefreshEnabled="True" IsRefreshing="{Binding IsBusy}" RefreshCommand="{Binding PullRefreshCommand}" >
<ListView.Behaviors>
<helperMethods:EventToCommandBehavior EventName="ItemSelected"
Command="{Binding OnSelectedIssueCommand}"
Converter="{StaticResource SelectedItemConverter}" />
</ListView.Behaviors>
<ListView.ItemTemplate>
<DataTemplate>
<ViewCell x:Name="Stalin">
<ViewCell.ContextActions>
<MenuItem x:Name="Hitler" Command="{Binding Path=BindingContext.OnDelete, Source={pageModels:IssueListPageModel}}" Text="Delete" IsDestructive="True" />
</ViewCell.ContextActions>
<ViewCell.View>
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="Auto"></RowDefinition>
<RowDefinition Height="Auto"></RowDefinition>
<RowDefinition Height="*"></RowDefinition>
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="70"></ColumnDefinition>
<ColumnDefinition Width="*"></ColumnDefinition>
<ColumnDefinition Width="50"></ColumnDefinition>
</Grid.ColumnDefinitions>
<Image Grid.Row="0" Grid.RowSpan="3" Grid.Column="0" Source="{Binding SeverityImagePath}" HorizontalOptions="Center" VerticalOptions="Center" HeightRequest="70"/>
<Image Grid.Row="0" Grid.RowSpan="3" Grid.Column="2" Source="{Binding StatusImagePath}" HorizontalOptions="Center" VerticalOptions="Center" HeightRequest="60"/>
<Label Grid.Row="0" Grid.Column="1" Text="{Binding Title}" LineBreakMode="TailTruncation" YAlign="Center" VerticalOptions="Start" Font="Bold, Medium"/>
<Label Grid.Row="1" Grid.Column="1" Text="{Binding Created, Converter={StaticResource DateToTextConverter}}" YAlign="Center" VerticalOptions="Start" Font="Medium"/>
<Label Grid.Row="2" Grid.Column="1" Text="{Binding Description}" LineBreakMode="WordWrap" YAlign="Start" VerticalOptions="Start" Font="Small"/>
</Grid>
</ViewCell.View>
</ViewCell>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
推荐答案
如果我理解您的问题,那应该很容易.
Should be quite easy, if I understand your problem.
将页面的BindingContext
设置为包含Command
且其下一个名为"MyRootLevelCommand"的ViewModel
.
Set your page's BindingContext
to your ViewModel
that contains a Command
with the next name "MyRootLevelCommand".
命名您的根UI元素:
<Grid x:Name="root"> .. </Grid>
让我们假设在此Grid
中,您有一个ListView
,并且想要将Command
从页面视图模型绑定到ListView中的每个项目:
Let's assume that within this Grid
you have a ListView
and you want to bind a Command
from the page viewmodel to each item in the ListView:
<ListView.ItemTemplate>
<DataTemplate>
<ViewCell>
<Button Command="{Binding Source={x:Reference root}, Path=BindingContext.MyRootLevelCommand}" />
</ViewCell>
</DataTemplate>
</ListView.ItemTemplate>
这篇关于如何绑定到视图模型而不是页面的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!