2016-12-09 10 views
0

Xamarin.FormsのListViewのカスタムテンプレートを作成しようとしています。テンプレートの中で私はテンプレートも入力 "MainTemplate"という名前のプロパティを持つカスタムビューを配置します。問題は、MainTemplateに1つ以上のデータバインディングが含まれている場合、それらは無視されます(すべてのXAMLがロードされますが、バインドされたデータは失われます)。私のテンプレートの中には、スコープが失われているように見えます。ビューから他のビューへのデータバインド

リストビュー:

 <ListView ItemsSource="{Binding items}"> 
     <ListView.ItemTemplate> 
      <DataTemplate> 
       <ViewCell> 
        <StackLayout> 
         <c:GestureListItem> 
          <c:GestureListItem.MainTemplate> 
           <ContentView BackgroundColor="Red" Padding="10"> 
            <Label Text="{Binding Name}" /> 
           </ContentView> 
          </c:GestureListItem.MainTemplate> 
         </c:GestureListItem> 
        </StackLayout> 
       </ViewCell> 
      </DataTemplate> 
     </ListView.ItemTemplate> 
    </ListView> 

マイカスタムテンプレート:

<?xml version="1.0" encoding="UTF-8"?> 
<Grid xmlns="http://xamarin.com/schemas/2014/forms" xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml" x:Class="Xamtools.GestureListItem"> 
    <ContentView Content="{Binding MainTemplate}" x:Name="mainTemplate" /> 
</Grid> 

...と背後にそのコード:

public partial class GestureListItem : Grid 
{ 
    public static readonly BindableProperty MainTemplateProperty = BindableProperty.Create("MainTemplate", typeof(View), typeof(GestureListItem), null, propertyChanged: MainTemplatePropertyChanged); 
    private static void MainTemplatePropertyChanged(BindableObject bindable, object oldValue, object newValue) 
    { 
     var ob = bindable as GestureListItem; 
     ob.MainTemplate = (View)newValue; 
    } 

    public View MainTemplate { set { SetValue(MainTemplateProperty, value); } get { return (View)GetValue(MainTemplateProperty); } } 
    public GestureListItem() 
    { 
     InitializeComponent(); 
     this.mainTemplate.BindingContext = this; 
    } 
} 

答えて

関連する問題