2010-12-15 105 views
0

Je dois obtenir la propriété SelectedValue d'une ComboBox Silverlight 4 en tant que DependencyPproerty via Reflection, mais je ne suis pas sûr de savoir comment procéder.ComboBox SelectedValue via Reflection comme DependencyProperty

myComboBox.GetType().GetFields() 

renvoie DependencyProperties mais seulement quatre des propriétés du ComboBox sont renvoyés et SelectedValue n'est pas un d'entre eux.

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

obtient la propriété mais il s'agit d'un objet System.Object et non d'un objet DependencyObject. Je cherche finalement à obtenir les liaisons pour le contrôle, ce qui nécessite un DependencyProperty pas un objet.

Edit:

Cela se passe dans un comportement et je ne sais pas ce que le contrôle est, je travaille avec un contrôle ComboBox en ce moment. Tout ce que j'ai est une chaîne passée de XAML. Dans WPF, je pourrais utiliser mySource="{x:Static ComboBox.SelectedValueProperty}" en tant que DependencyProperty mais Silverlight n'a pas x:Static en XAML. Donc, je suis en train de convertir mySource="SelectedValue" en DependencyProperty.

Répondre

2

Est-ce que cela fonctionne pour vous?

myComboBox.GetValue(ComboBox.SelectedValueProperty); 

--EDIT--

Pour obtenir un DependencyProperty de tout type Control, utilisez le code suivant:

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--

code suivant fonctionne pour moi dans un 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

En fait, ce qui se passe dans un comportement et je ne sais pas ce que le contrôle est, Je travaille avec un contrôle ComboBox en ce moment. Tout ce que j'ai est une chaîne passée de XAML. Dans WPF, je pourrais utiliser mySource = "{x: Static ComboBox.SelectedValueProperty}" comme DependencyProperty mais Silverlight n'a pas de x: Static dans XAML. Donc j'essaye de convertir mySource = "SelectedValue" en DependencyProperty. – strattonn

+0

Cela semble bien, le seul problème est GetProperty ("SelectedValueProperty") renvoie une valeur nulle, GetProperty ("SelectedValue") renvoie un objet non DependencyProperty. (GetFields a besoin de "Propriété" à ajouter.) – strattonn

+0

C'était une erreur. Code mis à jour pour utiliser 'GetField' à la place. – decyclone

0

La propriété est en fait intitulé SelectedValueProperty mais si vous essayez d'obtenir les liaisons de contrôle tentent cette ...

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

Oui, ComboBox.SelectedValue est un DP et c'est là que j'essaie d'aller mais c'est un comportement, donc l'utilisateur passe le nom d'une propriété dans via une chaîne (voir la modification ci-dessus). J'ai donc besoin de convertir une chaîne en un DP. – strattonn