2010-12-15 7 views
0

Reflectionを介してDependencyPproertyとしてSilverlight 4 ComboBoxのSelectedValueプロパティを取得する必要がありますが、これを行う方法がわかりません。ComboBox SelectedValue via Reflection as DependencyProperty

myComboBox.GetType().GetFields() 

返しDependencyPropertiesが、コンボボックスのプロパティの4つだけが返され、SelectedValueのはそのうちの一つではありません。

myComboBox.GetType().GetProperty("SelectedValue") 

はプロパティを取得しますが、それはSystem.Objectであり、DependencyObjectではありません。

私は最終的にオブジェクトではなくDependencyPropertyを必要とするコントロールのバインディングに到達しようとしています。

編集:これは、動作中に起こっていると私はコントロールが何であるかを知らない

、私は今、ComboBoxコントロールで働いています。私が持っているのは、XAMLから渡された文字列です。 WPFでは、私はmySource="{x:Static ComboBox.SelectedValueProperty}"をDependencyPropertyとして使うことができましたが、SilverlightはXAMLでx:Staticを持っていませんでした。だから私はmySource="SelectedValue"をDependencyPropertyに変換しようとしています。

答えて

2

これは機能しますか?

DependencyProperty property = control.GetType().GetField(propertyName + "Property", 
      BindingFlags.Public | BindingFlags.Static | BindingFlags.FlattenHierarchy).GetValue(control) as DependencyProperty; 

BindingExpression bindingExpression = control.GetBindingExpression(property); 

// Use bindingExpression.ParentBinding 

--edit 2--

次のコードは、のために働く:

--EDIT--

myComboBox.GetValue(ComboBox.SelectedValueProperty); 
は、次のコードいかなる Control種類、使用から DependencyPropertyを取得するには私の中で Silverlight 4 Application

Control control = new ComboBox(); 
String propertyName = "SelectedValue"; 

DependencyProperty property = control.GetType().GetField(propertyName + "Property", 
     BindingFlags.Public | BindingFlags.Static | BindingFlags.FlattenHierarchy).GetValue(control) as DependencyProperty; 

BindingExpression bindingExpression = control.GetBindingExpression(property); 
// bindingExpression will be null since we just created a `ComboBox`. It does not have any bindings yet. 
+0

SelectedValuePropertyというタイトルけどさ私は今ComboBoxコントロールで作業しています。私が持っているのは、XAMLから渡された文字列です。 WPFでは、mySource = "{x:Static ComboBox.SelectedValueProperty}"をDependencyPropertyとして使用できますが、SilverlightにはXAMLではx:Staticがありません。だから、mySource = "SelectedValue"をDependencyPropertyに変換しようとしています。 – strattonn

+0

GetProperty( "SelectedValueProperty")はnullを返し、GetProperty( "SelectedValue")はDependencyPropertyではないオブジェクトを返します。 (GetFieldsは "Property"を追加する必要があります) – strattonn

+0

間違いでした。代わりに 'GetField'を使用するようにコードが更新されました。 – decyclone

0

あなたがコントロールのバインディングはこれを試して取得しようとしている場合プロパティは、実際に、実はこれは行動で起こっていると私はコントロールが何であるかを知らない...

BindingExpression expression = myComboBox.GetBindingExpression(ComboBox.SelectedValueProperty); 
+0

ええ、コンボボックス。SelectedValueはDPで、これは私が行おうとしているところですが、これはBehaviorです。したがって、ユーザーは文字列を介してプロパティの名前を渡しています(上記の編集を参照)。だから私はDPに文字列を変換する必要があります。 – strattonn

関連する問題