亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

如何將命令從 ViewModel 綁定到位于自己文件中的 DataTemplate

如何將命令從 ViewModel 綁定到位于自己文件中的 DataTemplate

PHP
SMILET 2024-01-20 21:17:18
我正在使用帶有 ItemTemplateSelector 的可綁定堆棧布局。我的數據模板位于另一個文件中,該文件是作為 MergedResourceDictionay 包含在 MainView 中的 ResourceDictionary。在我的一個 DataTemplates 中,我有帶有 TapGestureRecognizer 的標簽,它應該在 MainViewViewModel 中觸發命令,但我似乎無法開始工作......我嘗試Source={x:Reference MainPage}在我的命令綁定中使用它,但無法引用它,因為它不在同一個文件中(Xamarin.Forms.Xaml.XamlParseException:“位置 28:51。找不到 MainPage 引用的對象”)<--! this is snippet from MainPage  --> <ScrollView Orientation="Vertical" Grid.Row="1">            <local:BindableStackLayout BindableLayout.ItemsSource="{Binding Day.TodayEntry}"                     x:Name="BindableStack" Spacing="10" Margin="10"                            BindableLayout.ItemTemplateSelector="{StaticResource CardDetailTemplateSelector}"/>        </ScrollView><--! this is problematic snippet from data template  --><Label Text="REMOVE" FontSize="Medium" TextColor="White" HorizontalOptions="End" Margin="3,0,0,0">            <Label.GestureRecognizers>                            <TapGestureRecognizer Command="{Binding RemoveEntryCommand, Source={x:Reference MainPage}}"                               CommandParameter="{Binding .}"/>            </Label.GestureRecognizers></Label>
查看完整描述

1 回答

?
慕俠2389804

TA貢獻1719條經驗 獲得超6個贊

不起作用的原因是 XAML 編譯查找是特定于頁面的,這意味著如果您有兩個不同的頁面并且使用名稱進行搜索,則在大多數情況下它將不起作用。

在這種情況下,我通常所做的就是為 BindingContext 使用parent關鍵字!

因此,假設您的標簽(位于 ViewCell 中)的父級是 Grid,您可以執行類似的操作

Command="{Binding Source={x:Reference parentLayoutGrid}, Path=Parent.Parent.BindingContext.RemoveEntryCommand}"

所需的父屬性數量取決于您的 ViewHeirarchy 以及哪個 View 將具有正確的上下文。

祝你好運。

如有疑問,請隨時回復


查看完整回答
反對 回復 2024-01-20
  • 1 回答
  • 0 關注
  • 147 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號