2016-11-11 11 views
0

私は2つの異なるセルの中から選択するDataTemplateSelectorを持っています。 Androidでは、このテンプレートはAndroid xmlファイルとして定義されたセルを選択します。 2つの異なる色の円が表示され、色が正しいため、テンプレートセレクターが機能していることを確認できます。しかし、私のデータは拘束されていませんし、私はなぜそれがわかりません。私はバインディングをどこかに設定していないと思うが、どこで/どうすればいいのか分からない。DataTemplateSelectorを使用している場合、カスタムレンダラーを使用してカスタムセルのバインディングを設定する場所

ここでは、ListViewとDataTemplateSelectorが含まれているページがあります。 ItemsSourcehereを設定しましたが、リストアイテムの異なる部分のバインディングは決して設定しません。それは私が何をすべきかわからないところです。

<ContentPage xmlns="http://xamarin.com/schemas/2014/forms" 
      xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml" 
      x:Class="MyApp.Pages.Routines.TopLevelRoutinesPage" 
      xmlns:statics="clr-namespace:MyApp.Statics;assembly=MyApp" 
      xmlns:controls="clr-namespace:MyApp.Controls;assembly=MyApp"> 
    <ContentPage.Resources> 
    <ResourceDictionary> 
     <controls:RoutinesDataTemplateSelector x:Key="RoutinesDataTemplateSelector"></controls:RoutinesDataTemplateSelector> 
    </ResourceDictionary> 
    </ContentPage.Resources> 
    <ContentPage.Content> 
    <StackLayout VerticalOptions="FillAndExpand" 
       HorizontalOptions="FillAndExpand" 
       Orientation="Vertical" 
       Spacing="0"> 
     <ListView ItemsSource="{Binding SelectedRoutineTree}" 
       ItemTemplate="{StaticResource RoutinesDataTemplateSelector}" 
       x:Name="RoutinesView" 
       ItemSelected="RoutineClicked" 
       Margin ="0, 8, 0, 0"> 
     </ListView> 
    </StackLayout> 
    </ContentPage.Content> 
</ContentPage> 

コードビハインド:

using MyApp.ViewModels; 
using MyCloudContracts.DTOs; 
using System; 
using System.Linq; 
using Xamarin.Forms; 

namespace MyApp.Pages.Routines 
{ 
    public partial class TopLevelRoutinesPage : ContentPage 
    { 
     private TopLevelRoutinesViewModel _viewModel; 
     private string _projCompName; 

     public TopLevelRoutinesPage(Guid docId, bool fromCompany, string projCompName) 
     { 
      InitializeComponent(); 
      _projCompName = projCompName; 
      Title = _projCompName; 
      _viewModel = new TopLevelRoutinesViewModel(docId, fromCompany); 
      BindingContext = _viewModel; 

      if (Device.OS == TargetPlatform.Android) 
       RoutinesView.SeparatorVisibility = SeparatorVisibility.None; 
     } 

     private async void RoutineClicked(object sender, SelectedItemChangedEventArgs e) 
     { 
      //since this is also called when an item is deselected, return if set to null 
      if (e.SelectedItem == null) 
       return; 

      var selectedRoutine = (PublishedDocumentFragmentDTO)e.SelectedItem; 
      var fragId = selectedRoutine.FragmentId; 
      var title = selectedRoutine.Title; 
      var blobIdStr = selectedRoutine.BlobId; 
      var blobId = new Guid(blobIdStr); 

      if (selectedRoutine.Children.Any()) 
      { 
       var routineTree = _viewModel.SelectedRoutineTree; 
       var subroutinesPage = new SubroutinesPage(routineTree, fragId, title, blobId, _projCompName); 
       await Navigation.PushAsync(subroutinesPage); 
      } 
      else 
      { 
       var routinePage = new RoutinePage(title, blobId); 
       await Navigation.PushAsync(routinePage); 
      } 

      //take away selected background 
      ((ListView)sender).SelectedItem = null; 
     } 
    } 
} 

DataTemplateSelector

using MyApp.Pages.Routines.CustomCells; 
using MyCloudContracts.DTOs; 
using Xamarin.Forms; 

namespace MyApp.Controls 
{ 
    class RoutinesDataTemplateSelector : DataTemplateSelector 
    { 
     private readonly DataTemplate _folderDataTemplate; 
     private readonly DataTemplate _routineDataTemplate; 

     public RoutinesDataTemplateSelector() 
     { 
      _folderDataTemplate = new DataTemplate(typeof(FolderViewCell)); 
      _routineDataTemplate = new DataTemplate(typeof(RoutineViewCell)); 
     } 

     protected override DataTemplate OnSelectTemplate(object item, BindableObject container) 
     { 
      var chooser = item as PublishedDocumentFragmentDTO; 
      if (chooser == null) 
       return null; 
      else if (chooser.Children.Length == 0) 
      { 
       return _routineDataTemplate; 
      } 
      else 
      { 
       return _folderDataTemplate; 
      }  
     } 
    } 
} 

そして、私のカスタムViewCellsの1例。私はこれが私が間違っているところだと思うが、私はなぜそれがわからない。私はプロパティを作成しますが、正しく設定する方法はわかりません。

using Xamarin.Forms; 

namespace MyApp.Pages.Routines.CustomCells 
{ 
    public class RoutineViewCell : ViewCell 
    { 
     public static readonly BindableProperty TitleProperty = 
      BindableProperty.Create("Title", typeof(string), typeof(RoutineViewCell), ""); 

     public string Title 
     { 
      get { return (string)GetValue(TitleProperty); } 
      set { SetValue(TitleProperty, value); } 
     } 
    } 
} 

助け:)

答えて

0

ためのおかげで私は答えを見つけました。カスタムセルファイルでOnBindingContextChanged()を無効にする必要がありました。私の作業コードは次のようになります:

using Xamarin.Forms; 

namespace MyApp.Pages.Routines.CustomCells 
{ 
    public class RoutineViewCell : ViewCell 
    { 
     public static readonly BindableProperty TitleProperty = 
      BindableProperty.Create("Title", typeof(string), typeof(RoutineViewCell), ""); 

     public string Title 
     { 
      get { return (string)GetValue(TitleProperty); } 
      set { SetValue(TitleProperty, value); } 
     } 

     protected override void OnBindingContextChanged() 
     { 
      this.SetBinding(TitleProperty, "Title"); 
      base.OnBindingContextChanged(); 
     } 
    } 
} 
関連する問題