2017-08-15 46 views
0

我還是習慣Xamarin形式,所以我有下列控制所謂PopupFrame:Xamarin形式 - 我的ContentPresenter不會顯示其內容

PopupFrame.cs

[XamlCompilation(XamlCompilationOptions.Compile)] 
public partial class PopupFrame : ContentView 
{ 
    public static readonly BindableProperty PopupContentProperty = 
     BindableProperty.Create(nameof(PopupContent), typeof(View), typeof(PopupFrame)); 

    public View PopupContent 
    { 
     get { return (View)GetValue(PopupContentProperty); } 
     set { SetValue(PopupContentProperty, value); } 
    } 

    public PopupFrame() 
    { 
     InitializeComponent(); 
    } 
} 

PopupFrame.xaml

<?xml version="1.0" encoding="UTF-8"?> 
<ContentView xmlns="http://xamarin.com/schemas/2014/forms" 
      xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml" 
      x:Class="TestApp.Core.Controls.PopupFrame"> 
    <Frame> 
     <StackLayout> 
      <Label Text="--- TEST TITLE ---" /> 

      <ContentPresenter Content="{TemplateBinding PopupContent}" /> 
     </StackLayout> 
    </Frame> 
</ContentView> 

筆者認爲:

<popCtl:PopupFrame HorizontalOptions="Center" 
        VerticalOptions="Center"> 
    <popCtl:PopupFrame.PopupContent> 
     <ListView x:Name="ListUsers"> 
      <ListView.ItemTemplate> 
       <DataTemplate> 
        <ViewCell> 
         <ViewCell.View> 
          <Label Text="{Binding Name}" 
            HorizontalOptions="CenterAndExpand" 
            VerticalOptions="Center" /> 
         </ViewCell.View> 
        </ViewCell> 
       </DataTemplate> 
      </ListView.ItemTemplate> 
     </ListView> 
     </popCtl:PopupFrame.PopupContent> 
</popCtl:PopupFrame> 

那麼,有什麼情況是,當內容查看控制顯示,只有標籤(與文本 - 測試名稱 - 顯示,而不是ListView控件)。

我也嘗試用ContentView替換ContentPreseter,但結果相同:我的ListView不顯示。我確信數據確實存在於ListView的ItemsSource中(在代碼隱藏中設置)。

我的ContentView設置錯誤嗎?

回答

1

TemplateBinding只能用於從控件模板內部進行綁定。爲了讓你的綁定工作 - 你可以使用ReferenceExtension來引用父控制。

對於離,更新如下結合:

<ContentView xmlns="http://xamarin.com/schemas/2014/forms" 
      xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml" 
      x:Class="TestApp.Core.Controls.PopupFrame" 
      x:Name="_parent"> 
    <Frame> 
     <StackLayout> 
      <Label Text="--- TEST TITLE ---" /> 

      <ContentPresenter 
       Content="{Binding Path=PopupContent, Source={x:Reference _parent}}" /> 
     </StackLayout> 
    </Frame> 
</ContentView> 
+1

哇,X:參考肯定是我新的東西。這將解釋爲什麼RelativeSource在XF中缺失。 謝謝,工作就像一個魅力! – Maximus