IdentifiantMot de passe
Loading...
Mot de passe oublié ?Je m'inscris ! (gratuit)
Navigation

Inscrivez-vous gratuitement
pour pouvoir participer, suivre les réponses en temps réel, voter pour les messages, poser vos propres questions et recevoir la newsletter

Silverlight Discussion :

Binding du Width d'une ColumnDefinition !


Sujet :

Silverlight

  1. #1
    Invité
    Invité(e)
    Par défaut Binding du Width d'une ColumnDefinition !
    Salut tout le monde.

    Il faut binder la proprieté Width d'une colonne d'une grid donc de mon ColumnDefinition. Mais j'ai l'erreur AG_E_PARSER_BAD_PROPERTY_VALUE. je sais cela est dû du fait que la propriété Width de mon ColumnDefinition n'est pas une DependencyProperty.

    Y'aurait il pas une routine pour contourner le problème ?

    Merci.

  2. #2
    Invité
    Invité(e)
    Par défaut
    Je viens de découvrir que le type de cette proprieté est de type GridLenght. Bizarre non. Mais cela ne vas pas résoudre mon problème puisque ce n'est toujours pas une DP.

  3. #3
    Expert confirmé
    Avatar de Skyounet
    Homme Profil pro
    Software Engineer
    Inscrit en
    Mars 2005
    Messages
    6 380
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 38
    Localisation : Etats-Unis

    Informations professionnelles :
    Activité : Software Engineer
    Secteur : High Tech - Éditeur de logiciels

    Informations forums :
    Inscription : Mars 2005
    Messages : 6 380
    Par défaut
    Tu peux pas utiliser le binding sur une ColumnDefinition puisque ça n'hérite pas de FrameworkElement.

    http://msdn.microsoft.com/en-us/libr...72(VS.95).aspx

    Tu peux manipuler la taille en code par contre.

  4. #4
    Invité
    Invité(e)
    Par défaut
    Mais il y a quelqu'un qui a réussi à le faire en WPF ici. J'ai essayé de faire la même chose ça ne marche pas.

  5. #5
    Expert confirmé
    Avatar de Skyounet
    Homme Profil pro
    Software Engineer
    Inscrit en
    Mars 2005
    Messages
    6 380
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 38
    Localisation : Etats-Unis

    Informations professionnelles :
    Activité : Software Engineer
    Secteur : High Tech - Éditeur de logiciels

    Informations forums :
    Inscription : Mars 2005
    Messages : 6 380
    Par défaut
    Oui mais l'héritage de ColumnDefinition n'est pas le même en WPF et en Silverlight.

    Pas de binding pour ça en SL.

  6. #6
    Membre Expert Avatar de davcha
    Profil pro
    Inscrit en
    Avril 2004
    Messages
    1 258
    Détails du profil
    Informations personnelles :
    Âge : 44
    Localisation : France

    Informations forums :
    Inscription : Avril 2004
    Messages : 1 258
    Par défaut
    Utilises une propriété attachée.

  7. #7
    Invité
    Invité(e)
    Par défaut
    Citation Envoyé par davcha Voir le message
    Utilises une propriété attachée.
    C'est la seule solution qui me reste...

    Merci pour vos réponses.

  8. #8
    Invité
    Invité(e)
    Par défaut
    Salut,

    Excusez moi du retard de la maj.

    J'ai créer une AttachedProperty qui marche à merveille si la valeur qui lui est attribuée n'est pas le résultat d'une binding.

    Mais quand je la binde à un objet, j'ai l'erreur suivante : AG_E_PARSER_PROPERTY_NOT_FOUND.

    Une recherche sur le net m'a permis de savoir qu'il fallait mettre la définition du namespcae explicitement c'est à dire spécifier l'assembly auquel appartient la proprieté attachée. Ce que j'ai fait mais j'ai toujours la même erreur.

  9. #9
    Invité
    Invité(e)
    Par défaut
    Projet à titre d'exemple en pièce jointe:
    Dernière modification par Invité ; 08/10/2009 à 14h47.

  10. #10
    Rédacteur
    Avatar de Thomas Lebrun
    Profil pro
    Inscrit en
    Octobre 2002
    Messages
    9 161
    Détails du profil
    Informations personnelles :
    Âge : 43
    Localisation : France

    Informations forums :
    Inscription : Octobre 2002
    Messages : 9 161
    Par défaut
    http://msdn.microsoft.com/fr-fr/libr...th(VS.95).aspx

    Width n'est pas une Dependency Property donc tu ne peux pas faire de binding dessus.

  11. #11
    Membre Expert Avatar de davcha
    Profil pro
    Inscrit en
    Avril 2004
    Messages
    1 258
    Détails du profil
    Informations personnelles :
    Âge : 44
    Localisation : France

    Informations forums :
    Inscription : Avril 2004
    Messages : 1 258
    Par défaut
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    public class BindToEverythingBehavior
    {
    	#region Attached Property string Property
    	public static string GetProperty(DependencyObject obj)
    	{
    		return ( string )obj.GetValue(PropertyProperty);
    	}
    	public static void SetProperty(DependencyObject obj, string value)
    	{
    		obj.SetValue(PropertyProperty, value);
    	}
    	public static readonly DependencyProperty PropertyProperty =
    		DependencyProperty.RegisterAttached("Property", typeof(string), typeof(BindToEverythingBehavior), new UIPropertyMetadata(null));
    	#endregion
    	#region Attached Property object Value
    	public static object GetValue(DependencyObject obj)
    	{
    		return ( object )obj.GetValue(ValueProperty);
    	}
    	public static void SetValue(DependencyObject obj, object value)
    	{
    		obj.SetValue(ValueProperty, value);
    	}
    	public static readonly DependencyProperty ValueProperty =
    		DependencyProperty.RegisterAttached("Value", typeof(object), typeof(BindToEverythingBehavior),
    		new UIPropertyMetadata(null, BindToEverythingBehavior.OnValueChanged));
    	#endregion
     
    	static void OnValueChanged(DependencyObject obj, DependencyPropertyChangedEventArgs e)
    	{
    		string propertyName = GetProperty(obj);
    		if ( string.IsNullOrEmpty(propertyName) == false ) {
    			var prop = obj.GetType().GetProperty(propertyName);
     
    			if ( prop == null )
    				throw new ArgumentNullException("property '" + propertyName + "' not found");
     
    			prop.SetValue(obj, Convert.ChangeType(e.NewValue, prop.PropertyType), null);
    		}
    	}
    }
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    <Window x:Class="WpfApplication1.Window1"
    		xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    		xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    		xmlns:l="clr-namespace:WpfApplication1"
    		Title="Window1"
    		Height="600"
    		Width="600">
    	<Window.Resources>
    		<l:StrToDoubleConverter x:Key="strToDoubleConverter" />
    	</Window.Resources>
    	<Canvas>
    		<TextBox x:Name="txt"
    				 Height="21.96"
    				 Canvas.Left="12"
    				 Canvas.Top="12"
    				 Width="554" />
     
    		<Button  Canvas.Left="12"
    				 Canvas.Top="40"
    				 Width="554"
    				 l:BindToEverythingBehavior.Property="Height"
    				 l:BindToEverythingBehavior.Value="{Binding Text, ElementName=txt, Converter={StaticResource strToDoubleConverter}, UpdateSourceTrigger=PropertyChanged}" />
    	</Canvas>
    </Window>
    C'est pas du Silverlight, mais ça devrait passer aussi.

  12. #12
    Invité
    Invité(e)
    Par défaut
    Citation Envoyé par Thomas Lebrun Voir le message
    http://msdn.microsoft.com/fr-fr/libr...th(VS.95).aspx

    Width n'est pas une Dependency Property donc tu ne peux pas faire de binding dessus.
    C'est pour ça que je veux passer par une AttachedProperty

  13. #13
    Invité
    Invité(e)
    Par défaut
    Citation Envoyé par davcha Voir le message
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    public class BindToEverythingBehavior
    {
    	#region Attached Property string Property
    	public static string GetProperty(DependencyObject obj)
    	{
    		return ( string )obj.GetValue(PropertyProperty);
    	}
    	public static void SetProperty(DependencyObject obj, string value)
    	{
    		obj.SetValue(PropertyProperty, value);
    	}
    	public static readonly DependencyProperty PropertyProperty =
    		DependencyProperty.RegisterAttached("Property", typeof(string), typeof(BindToEverythingBehavior), new UIPropertyMetadata(null));
    	#endregion
    	#region Attached Property object Value
    	public static object GetValue(DependencyObject obj)
    	{
    		return ( object )obj.GetValue(ValueProperty);
    	}
    	public static void SetValue(DependencyObject obj, object value)
    	{
    		obj.SetValue(ValueProperty, value);
    	}
    	public static readonly DependencyProperty ValueProperty =
    		DependencyProperty.RegisterAttached("Value", typeof(object), typeof(BindToEverythingBehavior),
    		new UIPropertyMetadata(null, BindToEverythingBehavior.OnValueChanged));
    	#endregion
     
    	static void OnValueChanged(DependencyObject obj, DependencyPropertyChangedEventArgs e)
    	{
    		string propertyName = GetProperty(obj);
    		if ( string.IsNullOrEmpty(propertyName) == false ) {
    			var prop = obj.GetType().GetProperty(propertyName);
     
    			if ( prop == null )
    				throw new ArgumentNullException("property '" + propertyName + "' not found");
     
    			prop.SetValue(obj, Convert.ChangeType(e.NewValue, prop.PropertyType), null);
    		}
    	}
    }
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    <Window x:Class="WpfApplication1.Window1"
    		xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    		xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    		xmlns:l="clr-namespace:WpfApplication1"
    		Title="Window1"
    		Height="600"
    		Width="600">
    	<Window.Resources>
    		<l:StrToDoubleConverter x:Key="strToDoubleConverter" />
    	</Window.Resources>
    	<Canvas>
    		<TextBox x:Name="txt"
    				 Height="21.96"
    				 Canvas.Left="12"
    				 Canvas.Top="12"
    				 Width="554" />
     
    		<Button  Canvas.Left="12"
    				 Canvas.Top="40"
    				 Width="554"
    				 l:BindToEverythingBehavior.Property="Height"
    				 l:BindToEverythingBehavior.Value="{Binding Text, ElementName=txt, Converter={StaticResource strToDoubleConverter}, UpdateSourceTrigger=PropertyChanged}" />
    	</Canvas>
    </Window>
    C'est pas du Silverlight, mais ça devrait passer aussi.
    Ce que t'as mis c'est pour un cas général. Je peux le mettre en Silverlight sans problème.

    Mon nouveau problème n'est pas de savoir comment définir une AttachedProperty mais pourquoi mon AttachedProperty n'est pas bindable alors que ça marche à merveille en WPF.

  14. #14
    Invité
    Invité(e)
    Par défaut
    j'ai résolu mon problème. J'ai commis l'erreur en voulant attacher la proprieté sur une ColumDefiniton. Il fallait le mettre sur une Grid.

Discussions similaires

  1. Récuperer la largeur(width) d'une Div qui n'en a pas
    Par Okena dans le forum Général JavaScript
    Réponses: 2
    Dernier message: 17/09/2010, 10h12
  2. impossible de changer la width d'une colonne de DGV bindé
    Par Kyaan dans le forum Windows Forms
    Réponses: 2
    Dernier message: 17/07/2009, 00h11
  3. Réponses: 5
    Dernier message: 26/09/2007, 03h34
  4. Bind variable incluse dans une autre bind variable
    Par kinder_pingui dans le forum SQL
    Réponses: 3
    Dernier message: 28/11/2006, 10h35
  5. Max-width dans une image sous IE?
    Par Death83 dans le forum Balisage (X)HTML et validation W3C
    Réponses: 9
    Dernier message: 23/12/2005, 02h04

Partager

Partager
  • Envoyer la discussion sur Viadeo
  • Envoyer la discussion sur Twitter
  • Envoyer la discussion sur Google
  • Envoyer la discussion sur Facebook
  • Envoyer la discussion sur Digg
  • Envoyer la discussion sur Delicious
  • Envoyer la discussion sur MySpace
  • Envoyer la discussion sur Yahoo