2017-02-13 16 views
2

ControlTemplateで定義されたバインディングを、自分のモデルに対して機能させるのに問題があります。Xamarinフォーム - ControlTemplateへのバインド

以下のControlTemplateで、TemplateBindingを使用してCount(オリーブラベル)というプロパティにバインドしています。私はprescribed by this articleとしてParent.Countを使用していますが、の値はありません。Parent.Countもあります。が動作しています。

enter image description here

次のページはのControlTemplateを使用しています。私のViewModelが動作することを証明するために、Countプロパティにバインドされた灰色のLabelがあります。

enter image description here

結果の画面に注目してください。灰色のラベルにはCountプロパティが表示されています。 ControlTemplateのオリーブラベルに何も表示されていません。私はControlTemplateの中でラベルを作ることができますどのように

enter image description here

のViewModelからCountプロパティを表示しますか?

ビューモデル

namespace SimpleApp 
{ 
    public class MainViewModel : INotifyPropertyChanged 
    { 
     public MainViewModel() 
     { 
      _count = 10; 
      Uptick = new Command(() => { Count++; }); 
     } 

     private int _count; 
     public int Count 
     { 
      get { return _count; } 
      set 
      { 
       _count = value; 
       OnPropertyChanged("Count"); 
      } 
     } 

     public ICommand Uptick { get; private set; } 

     public event PropertyChangedEventHandler PropertyChanged; 
     protected virtual void OnPropertyChanged(string propertyName) 
     { 
      if (PropertyChanged != null) 
       PropertyChanged(this, new PropertyChangedEventArgs(propertyName)); 
     } 
    } 
} 

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:SimpleApp" 
      x:Class="SimpleApp.MainPage" 
      ControlTemplate="{StaticResource ParentPage}"> 
    <StackLayout> 
     <Button Command="{Binding Uptick}" Text="Increment Count" /> 
     <Label Text="{Binding Count}" BackgroundColor="Gray" /> 
    </StackLayout> 
</ContentPage> 

BindingContextをここでMainViewModelに設定されている

お知らせ背後にあるコード。私は独自のViewModelを使用する必要があります。

namespace SimpleApp 
{ 
    public partial class MainPage : ContentPage 
    { 
     public MainPage() 
     { 
      BindingContext = new MainViewModel(); 

      InitializeComponent(); 
     } 
    } 
} 

コントロールテンプレートあなたのControlTemplateで

<?xml version="1.0" encoding="utf-8" ?> 
<Application xmlns="http://xamarin.com/schemas/2014/forms" 
      xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml" 
      x:Class="SimpleApp.App"> 
    <Application.Resources> 

     <ResourceDictionary> 
      <ControlTemplate x:Key="ParentPage"> 

       <StackLayout> 
        <Label Text="{TemplateBinding Parent.Count}" BackgroundColor="Olive" /> 
        <ContentPresenter /> 
       </StackLayout> 

      </ControlTemplate> 
     </ResourceDictionary> 

    </Application.Resources> 
</Application> 
+0

+0

私はすでにそれを試みましたが、どちらもうまくいきません。 –

+0

また、NuGetsパッケージとXamarin自体のアップデートを確認しますか?私は自分のPCを再インストールしましたが、ここにはXamarinはいません。私はここでテストしたいと思います。 – Tony

答えて

7

次のコードを使用してください:

<Label Text="{TemplateBinding BindingContext.Count}" BackgroundColor="Olive" /> 

それは多分、BindingContextを、自動的にあなたのContentPageの子供に適用されていないようですがXamarinのバグかもしれません。

+0

それは働いた!どのようにしてこの情報を見つけましたか? – Heshan

関連する問題