2017-07-27 12 views
1

Silverlight(および他のXAMLベースのテクノロジ)には、GetBindingExpressionというメソッドがあり、特定の依存関係プロパティにどのバインディングがあるかを確認できます。このメソッドはFrameworkElementにあり、すべてのコントロールによってすべてのバインディング式にアクセスできます。例えばXamarinフォーム - GetBindingExpression

var selectedItemBindingExpression = GetBindingExpression(SelectedItemProperty); 

しかし、Xamarinフォームで同等があるようには思えません。 XamarinフォームのBindablePropertyプロパティからバインディング式を取得する方法はありますか?

答えて

4

私はBindingExpressionにアクセスするXamarin.Formsで利用可能な任意のパブリックAPIがあるとは思わない - しかし、あなたは関連するBindingので、BindingExpression

public static class BindingObjectExtensions 
{ 
    public static Binding GetBinding(this BindableObject self, BindableProperty property) 
    { 
     var methodInfo = typeof(BindableObject).GetTypeInfo().GetDeclaredMethod("GetContext"); 
     var context = methodInfo?.Invoke(self, new[] { property }); 

     var propertyInfo = context?.GetType().GetTypeInfo().GetDeclaredField("Binding"); 
     return propertyInfo?.GetValue(context) as Binding; 
    } 

    public static object GetBindingExpression(this Binding self) 
    { 
     var fieldInfo = self?.GetType().GetTypeInfo().GetDeclaredField("_expression"); 
     return fieldInfo?.GetValue(self); 
    } 
} 

使用例をアクセスするためにリフレクションを使用することができます - 結合ゲット - 式

var expr = this.GetBinding(TextProperty).GetBindingExpression(); 

使用例 - 結合パスを取得(更新07/27)

//to access path - you can directly use the binding object 
var binding = this.GetBinding(TextProperty); 
var path = binding?.Path; 
+0

優れています。私はこれまでにこのようなことをしてきました。 Xamarinチームは、何の理由もなく私たちからものを隠すのが好きです。 –

+0

これをテストします。 –

+1

BindablePropertyを取得する方法を説明するビットを追加することがありますか? _associatedObject.GetBinding(Entry.TextProperty)のような静的なエントリから見ている人は、 – Andrey

関連する問題