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 :

[SL4] ListBox multimode twoway


Sujet :

Silverlight

Vue hybride

Message précédent Message précédent   Message suivant Message suivant
  1. #1
    Membre très actif
    Homme Profil pro
    Développeur .NET
    Inscrit en
    Juillet 2003
    Messages
    127
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Bouches du Rhône (Provence Alpes Côte d'Azur)

    Informations professionnelles :
    Activité : Développeur .NET
    Secteur : High Tech - Éditeur de logiciels

    Informations forums :
    Inscription : Juillet 2003
    Messages : 127
    Par défaut [SL4] ListBox multimode twoway
    Bonjour,

    je cherche à faire une validation de formulaire,
    j'ai ma classe décrivant l'objet source du formulaire :
    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
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
     
    public class POItem
        {
            #region Attributs
            private int _id { get; set; }
            private string _text { get; set; }
            private DateTime _date { get; set; }
            private List<AnotherItem> _otherItem { get; set; }
            #endregion
     
            #region Propriétés
            /// <summary>
            /// id
            /// </summary>
            [Required()]
            [Display(Name = "id: *", Description = "id de l'item")]
            public int Id
            {
                get
                {
                    return this._id;
                }
                set
                {
                    if (value == -1)
                    {
                        throw new ValidationException("id obligatoire");
                    }
                    this._id = value;
                }
     
            }
            /// <summary>
            /// text
            /// </summary>
            [Required()]
            [Display(Name = "Text: *", Description = "Text")]
            public string Text
            {
                get
                {
                    return this._text;
                }
                set
                {
                    if (string.IsNullOrEmpty(value))
                    {
                        value = null;
                        throw new ValidationException("Texte obligatoire");
                    }
     
                    if ((this._text != value))
                        this._text = value;
                }
     
            }
            /// <summary>
            /// date
            /// </summary>
            [Required()]
            [Display(Name = "Date: *", Description = "Date")]
            public string Date
            {
                get
                {
                    return this._date;
                }
                set
                {
                    if (string.IsNullOrEmpty(value))
                    {
                        value = null;
                        throw new ValidationException("Date obligatoire");
                    }
     
                    if ((this._date != value))
                        this._date = value;
                }
     
            }
            /// <summary>
            /// Other
            /// </summary>
            [Required()]
            [Display(Name = "Autre : *", Description = "Autre")]
            public List<AnotherItem> OtherItem
            {
                get
                {
                    return this._otherItem;
                }
                set
                {
                    this._otherItem = value;
                }
     
            }
            #endregion
        }
    code behind je fais juste :
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
     
    ThePOItem = new POItem();
    this.LayoutRoot.DataContext = ThePOItem;
    dans le XAML :
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
     
    <ComboBox SelectedValue="{Binding Id, Mode=TwoWay, ValidatesOnExceptions=true}" ></ComboBox>
    <TextBox Text="{Binding Text, Mode=TwoWay, ValidatesOnExceptions=true}"  ></TextBox>
    <toolkit:DatePicker     SelectedDate="{Binding Date,Mode=TwoWay}" />
    <ListBox  SelectedItem="{Binding OtherItem, Mode=TwoWay, ValidatesOnExceptions=true}" SelectionMode="Multiple"></ListBox>
    pour la combobox, textbox, datepicker et listbox SelectionMode Single pas de problème çela fonctionne bien,
    pour la listbox SelectionMode Multiple cela ne fonctionne pas. (SelectedItems n'éxiste pas dans le XAML)

    y a t il une solution à par : http://stackoverflow.com/questions/1...n-in-viewmodel.
    je trouve ça très lourd à implémenter.

    Merci d'avance
    ALCINA

  2. #2
    Invité
    Invité(e)
    Par défaut
    Citation Envoyé par ALCINA Voir le message

    pour la combobox, textbox, datepicker et listbox SelectionMode Single pas de problème çela fonctionne bien,
    Est tu sûr que cela fonctionne correctement vu que ta classe n'implémente pas INotifyPropertyChanged ?

    Citation Envoyé par ALCINA Voir le message
    pour la listbox SelectionMode Multiple cela ne fonctionne pas. (SelectedItems n'éxiste pas dans le XAML)
    C'est normal SelectedItems n'est pas une DependencyProperty. Alors le moyen le plus simple pour pouvoir binder dans le Xaml est passer par les AttachedProperties comme dans le lien que t'as fourni et que je remets ici :http://stackoverflow.com/questions/1...n-in-viewmodel.

    Citation Envoyé par ALCINA Voir le message
    je trouve ça très lourd à implémenter.
    Qu'est ce que t'as pas compris ?

  3. #3
    Membre très actif
    Homme Profil pro
    Développeur .NET
    Inscrit en
    Juillet 2003
    Messages
    127
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Bouches du Rhône (Provence Alpes Côte d'Azur)

    Informations professionnelles :
    Activité : Développeur .NET
    Secteur : High Tech - Éditeur de logiciels

    Informations forums :
    Inscription : Juillet 2003
    Messages : 127
    Par défaut
    Es tu sûr que cela fonctionne correctement vu que ta classe n'implémente pas INotifyPropertyChanged ?
    je confirme ça marche sans

    Qu'est ce que t'as pas compris ?
    je trouve bizarre le fait que cela ne soit pas natif.
    d'ailleurs j'ai apporté une modification au code du lien :
    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
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
     
    public static class SelectedItems
        {
            private static readonly DependencyProperty SelectedItemsBehaviorProperty =
                DependencyProperty.RegisterAttached(
                    "SelectedItemsBehavior",
                    typeof(SelectedItemsBehavior),
                    typeof(ListBox),
                    null);
     
            public static readonly DependencyProperty ItemsProperty = DependencyProperty.RegisterAttached(
                    "Items",
                    typeof(IList),
                    typeof(SelectedItems),
                    new PropertyMetadata(null, ItemsPropertyChanged));
     
            public static void SetItems(ListBox listBox, IList list) { listBox.SetValue(ItemsProperty, list); }
            public static IList GetItems(ListBox listBox) { return listBox.GetValue(ItemsProperty) as IList; }
     
            private static void ItemsPropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
            {
                var target = d as ListBox;
                if (target != null)
                {
                    GetOrCreateBehavior(target, e.NewValue as IList);
                }
            }
     
            private static SelectedItemsBehavior GetOrCreateBehavior(ListBox target, IList list)
            {
                var behavior = target.GetValue(SelectedItemsBehaviorProperty) as SelectedItemsBehavior;
     
                behavior = new SelectedItemsBehavior(target, list);
                target.SetValue(SelectedItemsBehaviorProperty, behavior);
     
     
                return behavior;
            }
        }
     
        public class SelectedItemsBehavior
        {
            private readonly ListBox _listBox;
            private readonly IList _boundList;
     
            public SelectedItemsBehavior(ListBox listBox, IList boundList)
            {
                _boundList = boundList;
                _listBox = listBox;
                Change();
                _listBox.SelectionChanged += OnSelectionChanged;
            }
     
            private void Change()
            {
                _listBox.SelectedItems.Clear();
                foreach (var item in _boundList)
                {
                    _listBox.SelectedItems.Add(item);
                }
            }
     
            private void OnSelectionChanged(object sender, SelectionChangedEventArgs e)
            {
                _boundList.Clear();
     
                foreach (var item in _listBox.SelectedItems)
                {
                    _boundList.Add(item);
                }
            }
        }
    si je veux implémenter une validation sur le set de OtherItem bizzarement ça marche pas.

Discussions similaires

  1. [SL4] Problème d'ajustement ListBox dynamique
    Par Mawisis dans le forum Silverlight
    Réponses: 4
    Dernier message: 17/01/2012, 17h41
  2. DrawItem d'un listbox...
    Par scorpiwolf dans le forum C++Builder
    Réponses: 5
    Dernier message: 22/06/2007, 15h50
  3. [Listbox] ScrollBar Horizontal
    Par haleem dans le forum VB 6 et antérieur
    Réponses: 4
    Dernier message: 20/04/2005, 07h53
  4. [Canvas] Listbox, couleur et multiselect
    Par rbag dans le forum Composants VCL
    Réponses: 3
    Dernier message: 25/09/2002, 13h02
  5. Copier le texte d'un ListBox dans le clipboard.
    Par Clément[Delphi] dans le forum Composants VCL
    Réponses: 3
    Dernier message: 18/08/2002, 08h20

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