2016-11-16 14 views
0

大きなUserControlを作成しました。テスト目的のために、BindingContext = thisを設定しました。今私はBindablePropertyが必要なので、私はもうこのトリックを使うことはできないので、どうすればそれを反映させることができますか?
問題は、XAMLでUserControlを使用していて、BindingContext = thisを設定すると、SelectedDateにバインドされた要素が存在しないということです。UserControl自身へのバインド

UserControlが、私はXAMLで定義することができる唯一のことは、私は、私はシンプルを作成している

を行くための正しい方法を見つけることができませんどの

<?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="ComplexUserControl" /> 

のようなメインであることを、非常に複雑ですユーザーコントロール、あなたが見ることができるので、私は

class Test : Grid 
{ 
    public static readonly BindableProperty SelectedDateProperty = BindableProperty.Create("SelectedDate", typeof(DateTime), typeof(Test), defaultValue: DateTime.Now, defaultBindingMode: BindingMode.TwoWay); 

    public DateTime Date1 
    { 
     get { return DateTime.Today.AddDays(-1); } 
    } 

    public DateTime Date2 
    { 
     get { return DateTime.Today; } 
    } 

    public DateTime Date3 
    { 
     get { return DateTime.Today.AddDays(1); } 
    } 

    public Test() 
    { 
     InitializeComponent(); 
    } 

    private void InitializeComponent() 
    { 
     ColumnDefinitions.Add(new ColumnDefinition { Width = new GridLength(1, GridUnitType.Star) }); 
     ColumnDefinitions.Add(new ColumnDefinition { Width = new GridLength(1, GridUnitType.Star) }); 
     ColumnDefinitions.Add(new ColumnDefinition { Width = new GridLength(1, GridUnitType.Star) }); 

     Button btn1 = new Button(); 
     btn1.SetBinding(Button.FontSizeProperty, new Binding("SelectedDate", converter: new IsSelectedFontSizeConverter(), converterParameter: Date1)); 
     btn1.SetBinding(Button.TextProperty, new Binding("Date1", stringFormat: "dd")); 
     Children.Add(btn1, 0, 0); 

     Button btn2 = new Button(); 
     btn2.SetBinding(Button.FontSizeProperty, new Binding("SelectedDate", converter: new IsSelectedFontSizeConverter(), converterParameter: Date2)); 
     btn2.SetBinding(Button.TextProperty, new Binding("Date2", stringFormat: "dd")); 
     Children.Add(btn2, 1, 0); 

     Button btn3 = new Button(); 
     btn3.SetBinding(Button.FontSizeProperty, new Binding("SelectedDate", converter: new IsSelectedFontSizeConverter(), converterParameter: Date3)); 
     btn3.SetBinding(Button.TextProperty, new Binding("Date3", stringFormat: "dd")); 
     Children.Add(btn3, 2, 0); 
    } 
} 

を行うにしようとしています何それは別の日にSelectedDateを設定するために外部から可能でなければなりません。

答えて

1

はい、できます。 x:参照の使用。

<Button Text="{Binding Date3,Source={x:Reference testControl}" 
     FontSize="{Binding SelectedDate, Converter={StaticResource myConverter}, 
      ConverterParameter={Binding Date3,Source={x:Reference testControl}}}" /> 

以下のリンクは、この概念を理解するのに役立ちます。 https://developer.xamarin.com/guides/xamarin-forms/xaml/xaml-basics/data_binding_basics/

+0

私は 'testcontrol'をC#コードでどのように設定できますか? XAMLでUserControlを作成できないため(あまりにも多くの計算がXAMLで行うUserControlで行われています) –

+0

コントロールのx:name = "testControl"を設定する必要があります。 test.xamlページを作成します。

+0

これはxamlでのみ行うことができます –

関連する問題