2016-11-23 19 views
0

私は以下の2つのことをしようとしているサンプルプロジェクトがあります。すべてのプロパティは同じViewModelにあります。XamarinフォームListViewヘッダーの問題

私のviewmodelのプロパティにvisibilityプロパティをバインドして、HeaderTemplateのすべてのオブジェクトの表示をfalseに設定して、ヘッダーを非表示にしようとしています。これは動作していません。私が本当に欲しいのは、ヘッダーをオン/オフする方法です。コードはうまくいきますが、XAMLを好むでしょう。すべてのコードは以下の通りです。任意の提案をいただければ幸いです。

注:私はすでに、ViewModelにクラスのインスタンスをインスタンス化し、ヘッダーのバインディングをそのクラスのプロパティにバインドする各ヘッダーラベルと共に設定するように、すべてのヘッダープロパティを独自のクラスに入れてみました。これはどちらもうまくいかなかった。

XAMLコード

<?xml version="1.0" encoding="utf-8" ?> 
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms" 
     xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml" 
     xmlns:local="clr-namespace:SampleHeaderFooter" 
     x:Class="SampleHeaderFooter.MainPage"> 

<ContentPage.Content> 

    <!-- List of places --> 
    <ListView x:Name="lvPlaces" 
        VerticalOptions="StartAndExpand" 
        HorizontalOptions="StartAndExpand" 
        HasUnevenRows="True" 
        ItemsSource="{Binding GBSPlaceDetails}" 
        Header="{Binding HeaderText}"> 
     <ListView.HeaderTemplate> 
      <DataTemplate> 
       <StackLayout IsVisible="False"> 
        <Label x:Name="lblHeaderText" Text="{Binding .}" IsVisible="{Binding HeaderVisible}" TextColor="White" FontSize="Medium" FontAttributes="Bold" /> 
        <Label x:Name="lblHeaderLink" Text="Click here to change your settings." IsVisible="{Binding HeaderVisible}" 
          TextColor="Blue" FontSize="Medium" FontAttributes="Bold"> 
         <Label.GestureRecognizers> 
          <TapGestureRecognizer Tapped="btnSettings_Clicked"/> 
         </Label.GestureRecognizers> 
        </Label> 
       </StackLayout> 
      </DataTemplate> 
     </ListView.HeaderTemplate> 
     <ListView.ItemTemplate> 
      <DataTemplate> 
       <ViewCell> 
        <BoxView x:Name="bvLine" Grid.Row="0" Grid.Column="0" Grid.ColumnSpan="2" VerticalOptions="Center" HorizontalOptions="FillAndExpand" BackgroundColor="Black" HeightRequest="1" /> 
         <Label x:Name="lblPlaceName" Grid.Row="1" Grid.Column="0" Grid.ColumnSpan="2" BackgroundColor="Transparent" TextColor="Gray" FontAttributes="Bold" 
            Text="{Binding Title}" FontSize="Medium" Margin="0,4,0,4" VerticalOptions="Center" HorizontalOptions="FillAndExpand"/> 
       </ViewCell> 
      </DataTemplate> 
     </ListView.ItemTemplate> 
    </ListView> 
</ContentPage.Content>   

コード

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Text; 
using System.Threading.Tasks; 
using Xamarin.Forms; 

namespace SampleHeaderFooter 
{ 
public partial class MainPage : ContentPage 
{ 
    public MainPage() 
    { 
     InitializeComponent(); 
     PlacesVM lobj_PlaceVM = new PlacesVM(); 
     BindingContext = lobj_PlaceVM; 
    } 

    private void btnSettings_Clicked(object sender, EventArgs e) 
    { 

    } 
    } 
} 

の後ろViewModelClass

using System; 
using System.Collections.Generic; 
using System.Collections.ObjectModel; 
using System.Linq; 
using System.Text; 
using System.Threading.Tasks; 

namespace SampleHeaderFooter 
{ 
    public class GBSPlaceDetail 
    { 
     /// <summary> 
     /// Name/Title of the Place 
     /// </summary> 
     public string Title { get; set; } 
    } 

    public class PlacesVM 
    { 
     private bool ib_HeaderVisible = false; 
     private string is_HeaderText = ""; 

     /// <summary> 
     /// Indicates if the header should be visible. This only happens when a query was performed using the values as specified in the settings and no locations 
     /// were returned. 
     /// </summary> 
     public bool HeaderVisible 
     { 
      get 
      { 
       return ib_HeaderVisible; 
      } 

      set 
      { 
       ib_HeaderVisible = value; 
      } 
     } 

     /// <summary> 
     /// The Text that should appear in the header. 
     /// </summary> 
     public string HeaderText 
     { 
      get 
      { 
       return is_HeaderText; 
      } 
      set 
      { 
       is_HeaderText = value; 
      } 
     } 

    /// <summary> 
    /// A collection for Nearby Place objects. 
    /// </summary> 
    public ObservableCollection<GBSPlaceDetail> GBSPlaceDetails { get; private set; } 


     /// <summary> 
     /// Constructor for the Places View Model 
     /// </summary> 
     public PlacesVM() 
     { 
      this.GBSPlaceDetails = new ObservableCollection<GBSPlaceDetail>(); 

      this.GBSPlaceDetails.Add(new GBSPlaceDetail { Title = "Place 1" }); 
      this.GBSPlaceDetails.Add(new GBSPlaceDetail { Title = "Place 2" }); 
      this.GBSPlaceDetails.Add(new GBSPlaceDetail { Title = "Place 3" }); 
      this.GBSPlaceDetails.Add(new GBSPlaceDetail { Title = "Place 4" }); 
      this.GBSPlaceDetails.Add(new GBSPlaceDetail { Title = "Place 5" }); 
      this.GBSPlaceDetails.Add(new GBSPlaceDetail { Title = "Place 6" }); 

      HeaderVisible = false; 
      HeaderText = "This is my header text.";    
     } 

    } 
} 

答えて

1

あなたが設をクリックすることができますので、私は最初に表示されるようにヘッダーを設定gsテキスト。 変更はほとんど必要ありません。変化に上部のみコピーされたXAMLで

、残りはコードメインページに同じ

<ListView x:Name="lvPlaces" 
        VerticalOptions="StartAndExpand" 
        HorizontalOptions="StartAndExpand" 
        HasUnevenRows="True" 
        ItemsSource="{Binding GBSPlaceDetails}" 
        Header="{Binding .}"> 


     <ListView.HeaderTemplate> 
     <DataTemplate> 
      <StackLayout Orientation="Vertical" IsVisible="{Binding HeaderVisible}"> 
      <Label x:Name="lblHeaderText" Text="{Binding HeaderText}" TextColor="White" FontSize="Medium" FontAttributes="Bold" /> 
      <Label x:Name="lblHeaderLink" Text="Click here to change your settings." 
        TextColor="Blue" FontSize="Medium" FontAttributes="Bold"> 
       <Label.GestureRecognizers> 
       <TapGestureRecognizer Tapped="btnSettings_Clicked"/> 
       </Label.GestureRecognizers> 
      </Label> 
      </StackLayout> 
     </DataTemplate> 
     </ListView.HeaderTemplate> 

です:モデルのコードで

public partial class MainPage : ContentPage 
{ 
    PlacesVM lobj_PlaceVM; 

    public HiddenHeaderPage() 
    { 
     InitializeComponent(); 

     lobj_PlaceVM = new PlacesVM(); 
     BindingContext = lobj_PlaceVM; 
    } 

    private void btnSettings_Clicked(object sender, EventArgs e) 
    { 
     lobj_PlaceVM.HeaderVisible = !lobj_PlaceVM.HeaderVisible; 
    } 
} 

:そうです

public class PlacesVM : INotifyPropertyChanged 
{ 
    private bool ib_HeaderVisible = false; 
    private string is_HeaderText = ""; 

    /// <summary> 
    /// Indicates if the header should be visible. This only happens when a query was performed using the values as specified in the settings and no locations 
    /// were returned. 
    /// </summary> 
    public bool HeaderVisible 
    { 
     get 
     { 
      return ib_HeaderVisible; 
     } 

     set 
     { 
      ib_HeaderVisible = value; 
      OnPropertyChanged(); 
     } 
    } 

    public event PropertyChangedEventHandler PropertyChanged; 
    protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = null) 
    { 
     PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName)); 
    } 
    /// <summary> 
    /// The Text that should appear in the header. 
    /// </summary> 
    public string HeaderText 
    { 
     get 
     { 
      return is_HeaderText; 
     } 
     set 
     { 
      is_HeaderText = value; 
     } 
    } 

    /// <summary> 
    /// A collection for Nearby Place objects. 
    /// </summary> 
    public ObservableCollection<GBSPlaceDetail> GBSPlaceDetails { get; private set; } 


    /// <summary> 
    /// Constructor for the Places View Model 
    /// </summary> 
    public PlacesVM() 
    { 
     this.GBSPlaceDetails = new ObservableCollection<GBSPlaceDetail>(); 

     this.GBSPlaceDetails.Add(new GBSPlaceDetail { Title = "Place 1" }); 
     this.GBSPlaceDetails.Add(new GBSPlaceDetail { Title = "Place 2" }); 
     this.GBSPlaceDetails.Add(new GBSPlaceDetail { Title = "Place 3" }); 
     this.GBSPlaceDetails.Add(new GBSPlaceDetail { Title = "Place 4" }); 
     this.GBSPlaceDetails.Add(new GBSPlaceDetail { Title = "Place 5" }); 
     this.GBSPlaceDetails.Add(new GBSPlaceDetail { Title = "Place 6" }); 

     HeaderVisible = true; 
     HeaderText = "This is my header text."; 
    } 

} 
+0

感謝それを固定した。ヘッダー{Binding}を試してみたが、明らかに他のすべての正しいものと組み合わされていないことを誓う。おかげで再びユリ。 –

+0

更新 - まだ少しの問題があります。このサンプルでさえ - ヘッダーを隠すためのリンクをクリックすると、項目の行はページ上で完全に上に移動しません。いくつかのスペースは常にヘッダーによって占められているようです。それを修正する方法はありますか? –

+0

更新の解決策:私のヘッダーがスペースを取らないようにするには、ヘッダー(StackLayoutとラベル)のすべてのフィールドでIsVisible = "{Binding HeaderVisible}"を使用する必要がありました。また、ListViewからVerticalOptionsとHorizo​​ntalOptionsを削除する必要がありました。私がそれをしたら、ヘッダーは目に見えなければスペースを取らなかった。お役に立てれば。 –

関連する問題