2016-09-21 5 views
0

アプリケーションでカスタムのusercontrolを作成しましたが、そのプロパティ(BindableProperty)をViewModelにバインドしようとしましたが、私のためには機能しません。私は何か間違っているのですか? これはユーザーコントロールです。これは、テストプロジェクトの目的のためのカスタムステッパーです。それらの間のボタンと数量ラベルを「減らしてください」。 usercontrol内のバインドが完全に機能することに注意してください(コマンドバインディングなど)。XamarinでカスタムUsercontrolとViewModelがバインドされていませんか?

namespace UsercontrolBindingTest 
{ 
    using Usercontrols; 
    using Xamarin.Forms; 

    public class App : Application 
    { 
     public App() 
     { 
      MainPage = new ContentPage 
      { 
       BindingContext = new MainPageVM(), 
       Content = new StackLayout 
       { 
        VerticalOptions = LayoutOptions.Center, 
        HorizontalOptions = LayoutOptions.Center, 
        Children = 
        { 
         this.GetLabel("Title"), 
         this.GetCustomStepper(), 
         this.GetLabel("SelectedQuantity") 
        } 
       } 
      }; 
     } 

     private Label GetLabel(string boundedPropertyName) 
     { 
      var ret = new Label(); 
      ret.HorizontalOptions = LayoutOptions.CenterAndExpand; 
      ret.SetBinding(Label.TextProperty, new Binding(boundedPropertyName)); 

      return ret; 
     } 

     private CustomQuantityStepper GetCustomStepper() 
     { 
      var ret = new CustomQuantityStepper(); 
      var dataContext = this.BindingContext as MainPageVM; 

      ret.SetBinding(CustomQuantityStepper.QuantityProperty, new Binding("SelectedQuantity", BindingMode.TwoWay)); 

      return ret; 
     } 
    } 
} 

そして、私の簡単なのViewModel:

namespace UsercontrolBindingTest 
{ 
    using System.ComponentModel; 

    internal class MainPageVM : INotifyPropertyChanged 
    { 
     private int _selectedQuantity; 

     public int SelectedQuantity 
     { 
      get 
      { 
       return this._selectedQuantity; 
      } 

      set 
      { 
       this._selectedQuantity = value; 
       this.NotifyPropertyChanged(nameof(this.SelectedQuantity)); 
      } 
     } 

     public string Title { get; set; } = "ViewModel is bound"; 

     public MainPageVM() 
     { 
      this.SelectedQuantity = 0; 
     } 

     public event PropertyChangedEventHandler PropertyChanged; 

     private void NotifyPropertyChanged(string propName) 
     { 
      this.PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propName)); 
     } 
    } 
} 

私は何をしようとしていることはViewModelに

namespace UsercontrolBindingTest.Usercontrols 
{ 
    using System.Windows.Input; 
    using Xamarin.Forms; 

    public class CustomQuantityStepper : ContentView 
    { 
     public static readonly BindableProperty QuantityProperty = 
      BindableProperty.Create(nameof(Quantity), typeof(int), typeof(CustomQuantityStepper), 0, BindingMode.TwoWay); 

     public int Quantity 
     { 
      get 
      { 
       return (int)base.GetValue(QuantityProperty); 
      } 

      set 
      { 
       base.SetValue(QuantityProperty, value); 
       this.OnPropertyChanged(nameof(this.Quantity)); 
      } 
     } 

     public ICommand DecreaseQuantityCommand { get; private set; } 

     public ICommand IncreaseQuantityCommand { get; private set; } 

     public CustomQuantityStepper() 
     { 
      this.BindingContext = this; 

      this.DecreaseQuantityCommand = new Command(() => this.Quantity--); 
      this.IncreaseQuantityCommand = new Command(() => this.Quantity++); 

      this.DrawControl(); 
     } 

     private void DrawControl() 
     { 
      var quantityEntry = new Entry(); 
      quantityEntry.SetBinding(Entry.TextProperty, new Binding("Quantity", BindingMode.TwoWay)); 
      quantityEntry.WidthRequest = 50; 
      quantityEntry.HorizontalTextAlignment = TextAlignment.Center; 

      var increaseQuantityButton = new Button { Text = "+" }; 
      increaseQuantityButton.SetBinding(Button.CommandProperty, "IncreaseQuantityCommand"); 

      var decreaseQuantityButton = new Button { Text = "-" }; 
      decreaseQuantityButton.SetBinding(Button.CommandProperty, "DecreaseQuantityCommand"); 

      var ui = new StackLayout() 
      { 
       Orientation = StackOrientation.Horizontal, 
       Children = 
       { 
        decreaseQuantityButton, 
        quantityEntry, 
        increaseQuantityButton 
       } 
      }; 

      this.Content = ui; 
     } 
    } 
} 

ビューとVM間の結合が機能していることを証明してビューに結合QuantityPropertyです私は多くの異なったトップスとブログの投稿をチェックしましたが、私の問題の解決策を見つけることができませんでした。ここに助けを願って...ここ 付属するサンプルプロジェクト:https://1drv.ms/u/s!Apu16I9kXJFtl28YBjkfwDztT9j0

答えて

0

あなたは自分自身へCustomQuantityStepperBindingContextthis)を設定しています。そのため、バインドエンジンは、カスタムコントロールで "SelectedQuantity"という名前のパブリックプロパティを探しています。ではなく、ViewModelのです。

  • コントロールにBindingContextを設定せず、現在定義されているコンテキストを使用するようにしてください(うまくいけば)VMを指しています。
  • BindingSourcepropertyを設定し、正しいソース(VM)を指すようにします。
+0

ありがとうKrumelur!これは私が行方不明だったものです... – kkopieczek

関連する問題