問題:
我有一個ListView顯示物件串列的頁面。我DataTemplate按照 Microsoft 的創建帶有 Type 的 DataTemplate將其分離為它自己的檔案。我希望能夠編輯或洗掉此串列中的條目,因此我一直使用ViewCell'sContextActions MenuItem來提供此功能。這一直有效,直到我不得不將我的 DataTemplate 與ContentPage. 處理編輯和洗掉我所在的命令ContentPage的BindingContext,這是ViewModel。
我嘗試過的事情
多種格式的系結,bothFindAncestorBindingContext和FindAncestor RelativeSourceMode,多種系結方式AncestorType
{Binding BindingContext.EditCommand, Source={RelativeSource Mode=FindAncestor, AncestorType={x:Type ContentPage}}}
{Binding Source={RelativeSource Mode=FindAncestorBindingContext, AncestorType={x:Type views:LoginView}}, Path=EditCommand}
{Binding Source={RelativeSource AncestorType={x:Type vm:LoginVM}}, Path=BindingContext.EditCommand}
題:
我將如何ViewModel從 detached系結到我的命令DataTemplate?我曾嘗試使用RelativeSource系結,但從未觸發該命令,或者從未找到祖先。這是我的基本設定:
內容頁面(登錄視圖):
....
<ContentPage.BindingContext>
<vm:LoginVM x:Name="viewmodel"/>
</ContentPage.BindingContext>
<ContentPage.Content>
....
<Frame HasShadow="True" Padding="30" VerticalOptions="End" BackgroundColor="{DynamicResource PageBackgroundColor}">
<ListView ItemsSource="{Binding sessionList}" SelectedItem="{Binding SelectedItem}" HasUnevenRows="True" >
<ListView.ItemTemplate>
<DataTemplate>
<templates:LoginSessionTemplate/>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
</Frame>
</ContentPage.Content>
資料模板:
<ViewCell xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
xmlns:views="clr-namespace:PIM.View"
x:Class="PIM.DataTemplates.LoginSessionTemplate">
<ViewCell.ContextActions>
<MenuItem Command="{Binding Source={RelativeSource AncestorType={x:Type views:LoginView}}, Path=EditCommand}" CommandParameter="{Binding .}" Text="Edit"/>
</ViewCell.ContextActions>
....
模板和資料物件之間的系結很好,我只是在系結到ViewModel分離的DataTemplate. 使用上面的代碼不會觸發命令。出現背景關系選單并且按鈕的文本是正確的,但是該命令要么從未執行,要么從未找到ViewModel's BindingContext。到目前為止,我唯一的解決方案是將 包含在DataTemplate中ContentPage,這確實違背DataTemplate了首先將分離的目的。任何幫助表示贊賞,謝謝!
更新與解答
ToolmakerSteve 很友好地鏈接了一個執行緒,盡管鏈接的答案不起作用,但該執行緒的公認答案起作用了。由于某種原因FindAncestor在我的情況下不起作用,我必須ViewModel通過 XAML 層次結構來參考。我不確定這是否是 MVVM 的好習慣,但它正在起作用。
特爾;博士:
要ViewModel在 detatched 中參考您的DataTemplate,您必須遍歷 XAML 層次結構。為此,請給您的DataTemplate'sViewCell一個x:Name引數,然后在您的系結中使用該參考。系結將訪問ViewCell的父母。系結應該是這樣的:
{Binding Parent.Parent.BindingContext.MyCommand, Source={x:Reference ViewCellName}}
我只需要改變 DataTemplate
<ViewCell xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
xmlns:views="clr-namespace:PIM.View"
x:Class="PIM.DataTemplates.LoginSessionTemplate"
x:Name="VC">
<ViewCell.ContextActions>
<MenuItem Command="{Binding Parent.Parent.BindingContext.EditCommand, Source={x:Reference VC}}" CommandParameter="{Binding .}" Text="Edit"/>
</ViewCell.ContextActions>
....
uj5u.com熱心網友回復:
@ToolmakerSteve 在評論中提出的建議沒有幫助,但同一個執行緒的實際答案確實有幫助。我能夠ViewModel通過參考ViewCell的父級而不是使用FindAncestor.
你必須給你的DataTemplate'sViewCell一個x:Name引數才能參考它。接下來是確定在到達包含BindingContext
您要查找的物件的元素之前必須遍歷的層次結構多遠。就我而言,我第一次選擇上兩個級別并且它奏效了。這是我修改后的系結,VC我ViewCell的x:Name.
{Binding Parent.Parent.BindingContext.EditCommand, Source={x:Reference VC}}
我覺得FindAncestor在這種情況下不起作用很奇怪。也許這是一個錯誤?不管怎樣,謝謝你的幫助。
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/371332.html
標籤:列表显示 xamarin.forms 虚拟机 数据模板
上一篇:具有帶空格的字串的選擇引數
